I am having some really strange behaviour in ASP.NET MVC3. Basically I've written an HttpModule that fires on BeginRequest which potentially rewrites my URL using HttpContext.RewritePath(string). This works properly in most cases, but I've noticed that when I have a space in my URL, ASP.NET MVC behaves differently when rewriting.

Example URL:

http://www.mysite.com/my%20url

My rewriter is set up with Regex, similar to Helicon, where it may capture a group and use that captured group in the rewrite. An example rule is:

<add original="^/(.*)\?test$" rewritten="/$1" />

This would rewrite a URL such as http://www.mysite.com/my%20url?test to http://www.mysite.com/my%20url <-- note that the querystring goes away. This functions correctly.

And my MVC routing is set up to capture the my%20url part with a wildcard in order to map it to a RouteData value under the key of "navstate". The rule looks like:

"{*navstate}"

So, when I don't rewrite, my RouteDataDictionary contains:

key: "navstate" value: "my url"

When I DO rewrite, my RouteDataDictionary contains:

key: "navstate" value: "my%20url"

Note that it did not URL Decode the RouteData variable.

Does anyone have any ideas on this? The only thing that seems to make a difference is the use of HttpContext.RewritePath(string) in my HttpModule... Turning off the module ensures proper functionality. Manually decoding the URL prior to the rewrite call using HttpUtility.UrlDecode(string) also makes it work... but feels like a hack.

EDIT:

This seems to be a bug not with MVC, but with ASP.NET Routing, so I have updated the tags as such.

有帮助吗?

解决方案

I checked on this and it's as I thought. URL rewriting and Routing were not designed to be used together on the same request. Think of Routing as a special form of URL rewriting, so the two interfere with each other.

I know it feels like a bug because it's not working as you would expect, but this is working exactly as it's supposed to. Think about the pipeline and when your modules gets in there vs. the work that the routing module has already done. Things have to happen in order, so decoding happens, url rewriting happens, etc.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top