如何在asp.net mvc3路由中处理“+”?

时间:2012-02-21 07:19:35

标签: asp.net-mvc-3 routes

我的路线:

routes.MapRoute(
    "Tag",
    "tag/{t}/{action}",
    new { controller = "tag", action = "unsolved" }
    );

链接如:

<a href="/tag/@(Uri.EscapeDataString(tagName))/unsolved">@tagName</a>

如果tagName不包含“+”,则一切正常,否则,如“http://www.test.com/tag/c%2B%2B/unsolved"(%2B来自@(Uri.EscapeDataString) (“+”)),我收到错误:HTTP错误404.11 - 未找到

谁可以帮助我?谢谢!

2 个答案:

答案 0 :(得分:4)

您可以尝试在web.config的<system.webServer>部分中允许双重转义,因为我怀疑您只在IIS 7中而不是在Visual Studio开发服务器中遇到此问题(顺便说一句,您应该这样做在你的问题中提到过):

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
    <security>
        <requestFiltering allowDoubleEscaping="true"/>
    </security>
</system.webServer>

进一步阅读:http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx

答案 1 :(得分:0)

您的MVC Web应用程序生成包含任何字符(%,+,/等)的链接。因此,使用URL编码字符串并生成链接:

new UrlHelper(HttpContext.Current.Request.RequestContext)
       .RouteUrl("Tag", 
                 new { tagName= HttpContext.Current.Server.UrlEncode(tagName) };