问题
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