zoukankan      html  css  js  c++  java
  • MVC 请求参数中带有HTML会引发Validation异常 ("A potentially dangerous Request.Form value was detected from the client")

    MVC中客户端传值到服务器端时,如果客户端字符串含有“</>"字样时就会报“检测到有潜在危险”(

    "A potentially dangerous Request.Form value was detected from the client")的错误。

    如:从客户端("test<br/>ttt")中检测到有潜在危险的 Request.Form 值。


    解决方法A: 

    在对应的Action加上[ValidateInput(false)]属性就可以解决,去除验证。

    但似乎在.NET 4.0这个属性不工作,所以在web.config中systerm.web节点中加入

    <httpRuntime requestValidationMode="2.0"/> 


    解决方法B: 

     方法A还是稍显麻烦,在MVC3中有更好的解决办法,代码如下:

                NameValueCollection paramColl = null;
                // comment strings are html strings, so get the unvalidate version here
                if (0 == String.Compare(request.HttpMethod, "POST", true))
                {
                    paramColl = new FormCollection(request.Unvalidated().Form);
                }
                else if (0 == String.Compare(request.HttpMethod, "GET", true))
                {
                    paramColl = new NameValueCollection(request.Unvalidated().QueryString);
                }
                else
                {
                    paramColl = request.Params;

                } 

  • 相关阅读:
    hdu 4474 大整数取模+bfs
    Codeforces Mafia
    hdu 4750 Count The Pairs(并查集)
    zoj 3659 Conquer a New Region(并查集)
    zoj 3656
    poj 3678 Katu Puzzle(Two Sat)
    UVa 11235 RMQ
    hdu 4768 Flyer (二分)
    hdu 4762 Cut the Cake概率公式
    Ural 1046 Geometrical Dreams(解方程+计算几何)
  • 原文地址:https://www.cnblogs.com/dlbrant/p/2327797.html
Copyright © 2011-2022 走看看