我有一个遗留代码问题,需要我支持随机网址,就好像它们是对主页的请求一样.某些URL中包含的字符会生成错误"从客户端(&)检测到潜在危险的Request.Path值".该站点使用ASP.Net MVC 3(在C#中)编写,并在IIS 7.5上运行.
这是一个示例URL ...
http://mywebsite.com/Test123/This_&_That
Run Code Online (Sandbox Code Playgroud)
这是我如何设置我的全部路线(我有其他路线来捕捉特定页面)......
routes.MapRoute(
"Default", // Route name
"{garb1}/{garb2}", // URL with parameters
new { controller = "Website", action = "Home", garb1 = UrlParameter.Optional, garb2 = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
我在web.config文件中添加了以下内容...
<configuration>
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
<configuration>
Run Code Online (Sandbox Code Playgroud)
我还在应该捕获URL的操作中添加了ValidateInput属性...
public class WebsiteController : Controller
{
[ValidateInput(false)]
public ActionResult Home()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到错误.有什么想法吗?我错过了什么?现在我只是在我的本地开发服务器上运行(我还没有在生产中尝试过这些修复).
我理解为什么会这样,但我需要一个解决方法.我在StackOverflow上查看了一些其他问题,但没有一个是有用的.我不想在整个网站上禁用输入验证,因为这绝对是危险的.我只有一个(至少现在)我需要禁用输入验证的地方.
我用[ValidateInput(false)]属性修饰了Action方法,我用Html.Encode对字符串进行编码.但是,我仍然得到同样的错误.这是我的观点:
<div id="sharwe-categories">
<ul class="menu menu-vertical menu-accordion">
@foreach(var topLevel in Model)
{
var topLevelName = Html.Encode(topLevel.Name);
<li class="topLevel">
<h3>
@Html.ActionLink(topLevel.Name, "Index", "Item", new { category = topLevelName }, new {@class = "main"} )
<a href="#" class="drop-down"></a>
</h3>
<ul>
@foreach (var childCategory in topLevel.Children)
{
var childcategoryName = Html.Encode(childCategory.Name);
<li>@Html.ActionLink(childCategory.Name, "Index", "Item", new RouteValueDictionary { { "category", topLevelName }, { "subcategory", childcategoryName } }, null)</li>
}
</ul>
</li>
}
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
如您所见,没有用户输入.但是一些类别名称中有一些"危险"字符......任何解决方案?