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.

 

Published Monday, November 14, 2005 12:11 AM by ScottGu

Comments

# re: Url Mapper w/ Regex Support

Monday, November 14, 2005 2:21 PM by Jeff Atwood
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"!

# re: Url Mapper w/ Regex Support

Tuesday, November 15, 2005 12:33 AM by scottgu
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

# re: Url Mapper w/ Regex Support

Monday, December 5, 2005 2:48 PM by Danny Chen
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.

# re: Url Mapper w/ Regex Support

Thursday, July 27, 2006 4:23 AM by 仪表
Is there any way to rewrite URLs that don't end in .aspx?

# re: Url Mapper w/ Regex Support

Friday, July 28, 2006 12:21 AM by ScottGu

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

# re: Url Mapper w/ Regex Support

Thursday, November 30, 2006 3:04 PM by cube dweller

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

# re: Url Mapper w/ Regex Support

Sunday, December 17, 2006 5:28 PM by Teddy

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.

# re: Url Mapper w/ Regex Support

Thursday, January 25, 2007 5:49 PM by ReTox

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?

# re: Url Mapper w/ Regex Support

Tuesday, January 30, 2007 5:12 PM by Jatin

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.

# re: Url Mapper w/ Regex Support

Friday, March 2, 2007 10:21 AM by Rajay

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.