how to deal “+” in asp.net mvc3 routes?

两盒软妹~` 提交于 2019-12-23 05:19:24

问题


my routes:

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

the link like:

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

if the tagName not contain "+",all work well , else ,like "http://www.test.com/tag/c%2B%2B/unsolved"(%2B come from @(Uri.EscapeDataString("+")),i get the error: HTTP error 404.11 - Not Found

who can help me? thanks!


回答1:


You may try allowing double escaping in the <system.webServer> section of your web.config as I suspect that you are having this problem only in IIS 7 and not in the Visual Studio development server (which by the way you should have mentioned in your question):

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

Further reading: http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx




回答2:


Your MVC web application generates an link that include any character (%,+,/,etc.). So use URL encode the string and generate link:

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


来源:https://stackoverflow.com/questions/9373642/how-to-deal-in-asp-net-mvc3-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!