警告:我有一个MVC站点作为.Net WebForms站点中的一个区域。我提前解释说,在它揭示我的问题的情况下。MVC Double Encoding MvcHtmlString
现在我遇到的问题是我路由的值可能包含特殊字符,如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
所以我想最简单的解决办法是我的查询参数传递给它的路径值之前正确编码。这导致了双重编码情况。然后我发现了大约MvcHtmlString.Create
这是专门设计告诉路由系统不要重新编码字符串值。但是,它仍然是双重编码。
var customerNameEncoded = MvcHtmlString.Create(HttpUtility.HtmlEncode(model.Name));
var routeResult = RedirectToAction("ManageCustomer", new { customerName = customerNameEncoded });
return routeResult;
这是生成的网址:
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").
的web.config
为MVC地区的以下标签:validateRequest="false"
的web.config
对整个网站有以下几点:httpRuntime requestValidationMode="2.0"
任何想法,为什么这个字符串被双重编码,并如何阻止它这样做?
如果你使用的是MVC,你应该使用[ValidateInput(false)]来修饰你的函数....我正在看你所说的两个链接,一个抛出模板错误,另一个不。但是,它们看起来一样。这是故意的吗? –