Trevor Daniel
Trevor Daniel

Reputation: 3974

Umbraco - Dynamic Pages

I was hoping someone might be able to give me some help with what I am planning to do...

I want to create a dynamic "City.aspx" page that accepts a url parameter and dynamically generates a page based on that particular city.

For example, if someone called "City.aspx?city=london" then it would build a page with custom content relating to London and if someone called the page "City.aspx?city=manchester" it would build the page with content relating to Manchester.

I have looked into building the sitemap and UrlRewriting and am pretty sure i can redirect to this new page with a parameter but have no idea what I need to do next.

Can anyone help please?

Thanks

TaxiRoute

Upvotes: 0

Views: 2338

Answers (2)

BeaverProj
BeaverProj

Reputation: 2215

If you have the information outside of standard Umbraco content that is dynamic for each city, then simply write a macro or macros (or partial views?) to get the dynamic data via that "city" get parameter. Then you can use UrlRewriting to make URLs look like standard web pages (/city/london.aspx). UrlRewriting will make that URL appear to the server as though it was this: /city.aspx?city=london. (http://our.umbraco.org/wiki/reference/packaging/package-actions/community-made-package-actions/add-an-url-rewrite-rule)

In your macros you can either pass the "city" get parameter to the macro(s) as a macro parameter via bracket syntax (http://our.umbraco.org/wiki/reference/templates/umbracomacro-element/macro-parameters/advanced-macro-parameter-syntax). Or your can just get the city parameter via request variables (razor) or Umbraco.library (XSLT).

Upvotes: 0

Martijn van der Put
Martijn van der Put

Reputation: 4092

I would recommend that you create url's like /city/london/1234 where the last part is the ID of your document. By using the built-in UrlRewrite function in Umbraco, you can make the url be internally rewritten to /city.aspx?name=london&id=1234 In the /config/Urlewriting.config you can add rewrite rules. For the above you need something like this:

 <add name="city_rewrite"
       virtualUrl="^~/city/(.*)/(.*)"
       rewriteUrlParameter="ExcludeFromClientQueryString"
       destinationUrl="~/city.aspx?name=$1&amp;cityid=$2"
       ignoreCase="true" />

Once you have this sorted out, you can use the following code in your code-behind off the City.aspx Macro to get the corresponding Document.

    // get the city Document Id from the querystring
    string cityID = HttpContext.Current.Request.QueryString["cityid"];

    if (!string.IsNullOrWhiteSpace(cityId))
    {
      // get the cityNode
      Node cityNode = new Node(cityId);
      // do whatever you want with this node, like display it's data
    }

This is a .NET Macro, but of course you can do the same with XSLT or Razor-code.

Upvotes: 2

Related Questions