开发者

Getting ASP.NET MVC to correctly escape the # (hash/pound) character in routes

I've got a route that looks like this:

routes.MapRoute(
"BlogTags",
"Blog/Tags/{tag}",
new { controller = "Blog", action = "BrowseTag", viewRss 开发者_如何学Python= false }
);

And I create a URL using that route like this:

<%= Html.RouteLink(Html.Encode(sortedTags[i].Tag), 
         new { action = "BrowseTag", tag = sortedTags[i].Tag })%>

However, when a tag with a # character (like "C#") is used, the routing engine doesn't escape it, so I get a URL that looks like this:

<a href="/Blog/Tags/C#">C#</a>

What I need is the # escaped so that it looks like this:

<a href="/Blog/Tags/C%23">C#</a>

I tried doing a Url.Encode on the tag before it went into the route, like this:

<%= Html.RouteLink(Html.Encode(sortedTags[i].Tag), 
         new { action = "BrowseTag", tag = Url.Encode(sortedTags[i].Tag) })%>

But that makes the routing engine double escape the # (which causes an ASP.NET crash with a bad request error)):

<a href="/Blog/Tags/C%2523">C#</a>

How can I get the routing engine to escape that # character for me correctly?

Thank you for your help in advance.


As a very bald solution, I would manually replace "#" with "%23" in the output of RouteLink. Provided you don't use fragments in your urls, it should work.

You could use regular expression to only apply replace to the last part of your url.


I have a similar SO question relating to "/". While researching that issue I learned that ASP.NET decodes the URL values before they get passed to the MVC framework, and since "#" has special meaning to URLs (just like the "/" I was dealing with) there's a good chance that something in the base routing engine is causing this behavior.

Like Levi mentioned in his comment, one solution is to use ASP.NET 4.0. Another solution would be to write a RouteLink helper that automatically replaces "#" with some marker (like "!MY_HASH_TOKEN!") and then reverse that replacement in your controller (or perhaps via a HttpModule of some sort).

Or, just throw in the towel and pass the tag value as a querystring argument. Not as sexy, but its simple and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜