Using CityDesk With Server Side Code Files

Many people think that CityDesk is for static HTML sites. That is purely a misconception. CityDesk is a content management system, and you can put your ASP or PHP files in the system just the same as you would put HTML files in the system. It's all content, right? You can use all of CityDesk's features - e.g. templates and CityScript etc. - with your server side code files.

The really cool part comes when you consider using CityScript to dynamically generate your server side code, which of course dynamically generates your web page! Just to be clear: CityDesk runs through your ASP source files that live in your CityDesk file, and carries out the logic dictated by any CityScript it finds. CityDesk then publishes this dynamically generated ASP file to the web server. When a visitor to your website requests that page, the web server in turn runs through the ASP page and carries out the logic dictated by the ASP script it finds. Layer upon layer of intelligence gives you lots of options. Keep in mind that although CityDesk runs on Windows only, you always have the option of publishing PHP files to an Apache web server running on Linux or Mac OSX.

This article focusses on setting up ASP.Net using CityDesk, but the concepts and techniques apply to any server side technology. We've put together a completely barebones .cty file that illustrates very simply how to use ASP.Net and UTF-8 within CityDesk. "Classic ASP", and generally speaking any other server side technology (e.g. be it Windows or Linux), would be very similar to implement. Click here to download the example file (54KB), you'll need to unzip it and then open it with CityDesk v. 2.0 or later.

  1. Publishing issues: Since any server side code must be served out of - and interpreted by - a web server, you must publish this CTY file to a folder served by your web server as an application. In IIS this means you need to "create" this directory as an "application".
  2. As with all server side code, to view the pages, you must send a request to the web server from a browser. In the sample file included with this article, the url is:

    http://localhost/aspx/default.aspx

    You can not simply view your server side files with a URL like the one below, because this is not viewing it through the lense of the web server:

    C:\Inetpub\wwwroot\aspx\default.aspx

    The CityDesk Location that this sample CTY file publishes to is:

    C:\Inetpub\wwwroot\aspx

    So in IIS, right click on that folder, select the properties, and click "create" to configure this as an ASP application. You can just leave all the default settings.

  3. Previewing issues: Note that there's never any point in trying to "preview" a server side code file, since when "previewing" you're not sending a request to the web server, you're reading the file (the source code!) directly from your hard drive. That just ain't how server-side code works (i.e. server-side code needs the "server"). When you want to publish your files to the web server, click Publish. But when CityDesk is done publishing the files, do not use the Preview option presented to you by that dialogue box. Instead, open a browser and point it to the web server itself, like this:  http://localhost/aspx/default.aspx  

  4. The Articles/Templates approach: There is an Article in this sample called "default". The problem with using an Article for an ASP page is that you must never view that Article in "Normal View", because Normal View demands that all source code be valid HTML, and since the ASP.Net source code is not valid HTML, Normal View will simply delete the invalid parts of the source code! (In version 2.0, that is.) You have been officially forewarned. You're fine if you only ever view the article in HTML view. If you open your article in Normal View by accident, just close it and don't save changes. Right click on the article and select "Open as HTML".

  5. An article in CityDesk that is meant to be an ASP.Net file must use a template that gives it a files extension of ".aspx". In the example file, there is such a template, it is named "aspx". The article named "default" uses this template, so that once it is published to the web server, it is reborn as a file named "default.aspx". If you're doing "Classic" ASP, use .asp instead, and if you're doing PHP, use a .php template, and so on.

  6. The raw files approach: A simpler approach may be to use raw .aspx files in CityDesk rather than CityDesk "articles" with their templates. Notice the "raw-aspx.aspx" file in this sample, with the same source code as the default article. You can't use templates with such non-articles, but you can use variables. So lets say you want the company logo to appear at the top of all your pages; you can put the HTML that displays the logo in a variable and put that variable at the top of all your non-articles. This gives you similar functionality to the templates, in that now you can change that particular HTML in one place and it will affect all the pages on your site.

  7. Using an external editor: If .aspx files are associated with Visual Studio on your computer, then when you double click on a .aspx file sitting in CityDesk, that file will open in Visual Studio. You can work on it there and when you save it, it is saved back into CityDesk. You can publish the file to your web server and view your changes without closing the file in Visual Studio. More on external editors.

    So, you can use either an Article/Template approach or a raw ASPX file approach, there are pros and cons to both.

  8. Encoding: Now, about the encoding! Very important. You need this setting in your web.config file:

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" />

    Here's more on ASP encoding, and here's more on encoding in general.

  9. You may also want to specify the following in your ASP file itself (if you're using the Article approach, put it at the top of your ASP template):

    <%@Page ... ResponseEncoding="UTF-8"  ... %>

  10. ASP.Net "CodeBehind" issues: You don't want to use the CodeBehind system of ASP.Net, because that requires that you compile everything every time you make a change. You want to use the src attribute in place of the codebehind attribute, so that you can make changes and have CityDesk publish your files and IIS will compile them on the fly. A complete discussion of this is beyond the scope of this article, but here is the way the attribute should look in the Page directive:

    <%@ Page ... src="default.aspx.cs" ... />

CityDesk Home

Fog Creek Home

Email Us