Chris Cavanagh’s Blog

ASP.NET Routing revisited (again)

October 18, 2008 · 10 Comments

I’ve updated my ASP.NET 3.5 + System.Web.Routing sample based on some great feedback and questions.  It’s much simpler and gives a better example of how routes can be configured.  It now uses Phil Haack’s excellent WebFormRouteHandler class, which is much more versatile than my unnecessarily generic one :)

You can run the sample here and grab the source code here and on CodePlex (I love the fact it supports SVN now).

The sample pages derive from a custom ‘RoutablePage’ class, but that’s not mandatory.  For simple routing, you can just derive from Page as usual.

If you’d like to use RequestContext’s GetVirtualPath method (to automatically build URLs based on defined routes) your page can implement IRoutablePage (or derive from RoutablePage which includes some useful helper methods).

Categories: .NET

10 responses so far ↓

  • System.Web.Routing with WebForms sample « Chris Cavanagh’s Blog // October 18, 2008 at 4:22 pm | Reply

    [...] April 25, 2008 · 15 Comments UPDATE 3: Updated and simplified sample project. [...]

  • Atom // November 14, 2008 at 1:32 am | Reply

    Someone tell me I’m not crazy for actually implementing this stuff on the rewrite of our company website. I feel like even the smart people are still figuring this stuff out…

    Livin’ on the edge, lemme tell ya.

  • Chris Cavanagh // November 14, 2008 at 8:01 am | Reply

    Atom – You’re not crazy for implementing it :)

    Generally [when developing .NET stuff] I prefer my code dependent on existing .NET functionality *or* Microsoft betas / CTPs, rather than 3rd party frameworks (URL rewriters etc). That’s just a personal preference though :)

    Note that later versions of ASP.NET will (apparently) have routable Webforms more tightly integrated & supported. I don’t anticipate too many breaking changes; you’ll hopefully just need a little less code to achieve it.

  • W // December 10, 2008 at 2:00 am | Reply

    Hi Chris, by chance, did you ever use URL Routing for calling Handlers? I tried it but Session State doesn’t work when I go through the Routing. Session State does work perfectly if I use the *.prefix instead.

  • Chris Cavanagh // December 10, 2008 at 6:11 am | Reply

    W – I’ve not tried it. If you’ve got a sample project I could try, please mail it (to code@chriscavanagh.com)

  • Issa Qandil // May 19, 2009 at 1:22 am | Reply

    Hi Chris,

    I was reading a couple of posts about webforms routing that you wrote and they are really helpful thanks.

    But am wondering if you got any idea about using sub-domains within routing

    I added to the RoutablePage this property letting the page itself dsfine the subdomain

    public string SubDomain
    {
    get
    {
    // Retrieve the url – and split by dots:

    var url = Request.Headers["HOST"];
    var index = url.IndexOf(“.”);

    // Determine if a subdomain is provided:
    if (index < 0)
    return string.Empty;

    string subDomain = url.Substring(0, index);

    if (subDomain.ToLower() == “www”)
    return string.Empty;

    // Get the subdomain (as a string):
    return subDomain;

    }
    }

    My problem is the BaseUrl keeps ignoring the Subdomain lets say the address is http://test1.localhost/ but the property keeps giving me http://localhost/ ignoring the subdomain.

    On the other side the output of the “SubDomain” property is correct ..

    Any ideas,
    Thanks

    • Chris Cavanagh // May 19, 2009 at 6:35 am | Reply

      Issa – Do you mean the BaseUrl property defined in RoutablePage? Currently it looks like this:

      return Request.Url.GetLeftPart( UriPartial.Authority ) + VirtualPathUtility.ToAbsolute( “~/” );

      I’m wondering if you just need to add a call to your SubDomain property?:

      return SubDomain + Request.Url.GetLeftPart( UriPartial.Authority ) + VirtualPathUtility.ToAbsolute( “~/” );

      Let me know if this helps :) (or if I’ve misunderstood your question)

  • Issa Qandil // May 19, 2009 at 6:42 am | Reply

    Thanks for you quick reply and sorry if i didnt make my question clear.

    Actually what i did to get to get things working within subdomains is :

    public string BaseUrl
    {
    get
    {
    string _baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(“~/”);
    _baseUrl = _baseUrl.Replace(“www.”, “”); //incase there was www … :P
    if (string.IsNullOrEmpty(SubDomain))
    return _baseUrl;
    else if (_baseUrl.Contains(SubDomain) && !string.IsNullOrEmpty(SubDomain))
    return _baseUrl;
    else
    return _baseUrl.Replace(Request.Url.Authority, SubDomain + “.” + Request.Url.Authority);
    }
    }

    So by keeping this in the master page
    <base href=”" />

    Everything now looks good but i still didn’t know why the Request.Url didn’t mention anything about the subdomains

  • Issa Qandil // May 19, 2009 at 11:30 am | Reply

    Yep i saw that article 2 days ago, but thanks for trying to help me with is.

    Am not sure if it would help since is is passing the subdomain as a parameter in his URL which make the subdomain useless.

    I will wait until i can find something better than the solution i had, maybe the .net team @ microsoft would update their rounting to support subdomain .

Leave a Comment