Url Mapper w/ Regex Support

Several people have asked why the built-in URL Mapper in ASP.NET 2.0 doesn't support regular expressions.  There were actually a few reasons for this -- one of the big ones being that just about the time we were about to consider adding it my team started also working on IIS7.  We realized that a full-featured version would want/need to take advantage of some of the new features in IIS7 as well as the support all content types (in particular -- images and directories).  So we postponed making it feature rich until a future version.

Christopher has built one you can use in the meantime with ASP.NET 2.0 though.  You can learn about it here.

 

10 Comments

  • Is there any way to rewrite URLs that don't end in .aspx?



    URL "rewriting" is kind of laughable when we require that they all end in ".aspx"!

  • Rich -- It only takes 20 lines to support a basic regex based URL rewriter. But if you want it to be secure and scalable it takes a lot more work.



    Specifically, you need to do a lot of work to make sure that you aren't succeptible to denial of service attacks where remote users send urls that cause computationally intensive regex queries to spin your CPUs.



    The 20 line sample you have in your website doesn't deal with this, and probably doesn't need to if it is only used on the occasional server. But Microsoft can't get away with that if we install it on 4 million servers (that would be too big a target).



    Also -- as Jeff mentioned a lot of people want the ability to-do non-dynamic request url rewrites where a url doesn't end in a file extension, or uses a nested URI path that doesn't map to a file-system directory. IIS6 and before doesn't make that easy without writing a ISAPI filter (and having this filter coordinate with a managed module is very, very difficult). With IIS7 we will be able to deliver a more comprehensive solution that handles *all* of these scenarios.



    Hope this helps explain why in more depth,



    Scott

  • Url mappings for non .aspx extensions can happen if you star-map to ASP.NET. The reason it doesn't seem to work with out the star mapping is that IIS doesn't map the request to ASP.NET.

  • Is there any way to rewrite URLs that don't end in .aspx?

  • The URL rewriter only runs with a request that is mapped to be processed by the ASP.NET ISAPI extension is executed. To handle other extensions, you'll want/need to configure IIS to have ASP.NET process it.

    Hope this helps,

    Scott

  • I just don’t get why it has taken so long for this issue to be addressed by MS.

  • Scott,
    Do you know of any url re-writers that don't affect AJAX processing? I seem to be having problems when I have ajax on a page with a re-written URL.

  • Can you please explain this:

    When I start asp.net 2.0 website from VS2005 with it's own development server, my URLrewriting works great: everything is handled properly, requests are mapped to aspx files as they should be.

    But, when I copy website to IIS, this doesn't work.

    Why? IIS is handling requests before asp.net?

  • ReTox - correct, IIS handles request before passing to your asp.net application. You need an ISAPI filter (see link in an earlier post) which will handle the request so it knows to pass it to your .aspx app.

  • When you add AJAX, you need to modify the line in Application_BeginRequest to looks like this:

    If Not Request.Path.IndexOf(".asmx") >= 0 Then
    Something.URLRewriter.Rewriter.Process()
    End If

    So the ajax request does not go to your rewriter process.

Comments have been disabled for this content.