注意:我有一个MVC站点作为.Net WebForms站点中的一个区域。我正在预先解释这件事,因为这件事对我的问题很有帮助。
现在我遇到的问题是,我正在路由可能包含特殊字符(如apostrophe (单引号))的值。但是,如果我没有正确地编码它所路由的值,那么当使用未编码的单引号作为筛选器时,Kendo MVC Grid会创建一个无效的模板。
http://{base}/{area}/{controller}/{view}?customerName=Justin's%20Instance - Throws Invalid Template Error
http://{base}/{area}/{controller}/{view}?customerName=Justin's%20Instance - No Error因此,我认为最简单的解决方案是在将querystring参数作为路由值传递之前正确地对其进行编码。这导致了双重编码的情况。然后,我发现了MvcHtmlString.Create,它是专门设计来告诉路由系统不要重新编码字符串值的。然而,它仍然是双重编码。
var customerNameEncoded = MvcHtmlString.Create(HttpUtility.HtmlEncode(model.Name));
var routeResult = RedirectToAction("ManageCustomer", new { customerName = customerNameEncoded });
return routeResult;这是创建的Url:
http://{base/{area}/{controller}/{view}?customerName=Justin%26%2339%3Bs%20Instance如您所见,“正在重新编码”。这将引发以下错误。
> System.Web.HttpRequestValidationException A potentially dangerous
> Request.QueryString value was detected from the client
> (customerName="Justin's Instance").MVC区域的web.config有以下标记:validateRequest="false"
整个网站的web.config有以下内容:httpRuntime requestValidationMode="2.0"
对于为什么这个字符串是双重编码的,以及如何阻止它这样做,有什么想法吗?
发布于 2013-05-30 19:34:00
这里有两种不同类型的编码。一个是HTML编码,它创建了这些转义序列。第二个是URL编码,它使%20转义序列。
我不知道Kendo网格是什么,我想我知道您的操作应该是什么结果。试试这段代码
public ActionResult Index()
{
var customerNameEncoded = "Justin's Instance";
var url = Url.Action("ManageCustomer", new { customerName = customerNameEncoded });
return Redirect(url);
}
public ActionResult ManageCustomer(string customerName)
{
ViewBag.CustomerName = customerName;
return View();
}如果在Index方法返回之前停止它,您将看到url内容是
"/Home/ManageCustomer?customerName=Justin%27s%20Instance"正确编码的URL是什么。
您还可以检查customerName在ManageCustomer操作中得到的是什么,您将看到
"Justin's Instance"不要对浏览器的地址栏中可能看到的内容感到困惑。有些浏览器(如Firefox)不显示编码的URL,可能会显示“./ManageCustomer?customerName=Justin‘s实例”之类的内容。
发布于 2013-06-02 11:05:23
1)查询字符串参数是URL编码的,而不是HTML编码的。(%表示法,而不是&#实体)
2)配置设置validateRequest和requestValidationMode适用于ASP.Net WebForms,而不是MVC。您必须将[ValidateInput(false)]属性添加到控制器方法中,正如Bryan评论的那样。
https://stackoverflow.com/questions/16843138
复制相似问题