我有一个默认的 Asp.Net 路由如下:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
没什么特别的。
我在 Home Controller 中有 super 简单的默认操作:
public ActionResult Index(string id)
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
我可以输入 URL:http://localhost:12143/Home/Index/HanselandCratel
它工作正常,但是当我输入时
http://localhost:12143/Home/Index/Hansel&Cratel
没有
我明白 & 必须进行编码,但是当我输入时:
http://localhost:12143/Home/Index/Hansel%26Cratel
它仍然不起作用我得到这个错误:
A potentially dangerous Request.Path value was detected from the client (&).
我知道在 web.config 中设置这个:
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="" />
但我担心这样做时我将不得不牺牲安全性。
除此之外还有其他选择吗?也许在 Asp.Net 中有任何设置?
最佳答案
I am aware of setting this in web.config:
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="" />
不要这样做,您将删除此请求验证规则提供的所有保护。如果您想允许 & 字符,则将所有其他字符留在原地:
<httpRuntime requestPathInvalidCharacters="<,>,*,%,:,\,?" />
but I am afraid I will have to sacrifice security when I do that.
这样 & 将被允许出现在你的请求 URL 中。请注意正确验证所有输入参数,并最终根据需要转义它们。请注意,它也应该使用原始规则就地完成......
您也可以重新包含其他字符,但我建议仅在需要时才这样做。您还可以添加新的:有时我将文本 ID 作为参数(用于 AJAX GET 请求),即使我确定我永远不会构建连接字符串的 SQL 命令...我通常添加 '(以及其他一些)。
Is there any other alternative to this? Perhaps any setting in Asp.Net?
是的,您可以回到 .NET 2.0 规则,但我认为没有理由这样做......
关于c# - Asp.Net MVC 路由不接受 & 在路由中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39369956/