【问题标题】:mvc - First "get" result for request.params get exception, second "get" is okmvc - request.params 的第一个“get”结果得到异常,第二个“get”没问题
【发布时间】:2012-08-19 11:14:16
【问题描述】:

mvc - request.params 的第一个“get”结果得到异常,第二个“get”没问题。 我有一个页面,其中有一个带有 xml 文本的字段。 我正在使用验证=假。 但是,在控制器中的 post 方法上,我试图获取 requset.params 并且我得到一个错误 “从客户端检测到潜在危险的 Request.Form 值”。经过一些挖掘和调试后,我发现我第一次尝试获取 request.params 时出现异常,但是当我第二次尝试获取它时一切正常。

这是我用来避免 xml 问题的过滤器(我将其转换为二进制数据并清空 xml 字符串字段):

   public class RestAPIAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                 ((SimulationModel)filterContext.ActionParameters["model"]).Data = CommonConverters.StringToByteArray(((SimulationModel)filterContext.ActionParameters["model"]).StringData);
            ((SimulationModel)filterContext.ActionParameters["model"]).StringData = string.Empty;


            base.OnActionExecuting(filterContext);
        }
    }

这是 post 方法:

   [HttpPost]
    [ValidateInput(false)]
    [RestAPIAttribute]
    public ActionResult EditSimulation(Guid id, SimulationModel model)
    {
        try
        {
            model.RelationModel = new RelationModel(false, this.Resource("Simulations.AddToObjects"), "SimulationToObjects", id, sessionId, Request.Params, new List<ObjectTypes>() { ObjectTypes.Entity, ObjectTypes.EntityType, ObjectTypes.Universe });
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }

        /* more code here*/

        return View(newModel);
    }

现在,您可以看到 RelationModel 的构造函数之一有一个 request.param 参数,这给了我问题。

我目前对这个问题的解决方法只是调用它两次(但我正在寻找更好的解决方案或至少一个解释):

   [HttpPost]
    [ValidateInput(false)]
    [RestAPIAttribute]
    public ActionResult EditSimulation(Guid id, SimulationModel model)
    {
        try
        {
             try
            {
                model.RelationModel = new RelationModel(false, this.Resource("Simulations.AddToObjects"), "SimulationToObjects", id, sessionId, Request.Params, new List<ObjectTypes>() { ObjectTypes.Entity, ObjectTypes.EntityType, ObjectTypes.Universe });
            }
            catch
            {
                model.RelationModel = new RelationModel(false, this.Resource("Simulations.AddToObjects"), "SimulationToObjects", id, sessionId, Request.Params, new List<ObjectTypes>() { ObjectTypes.Entity, ObjectTypes.EntityType, ObjectTypes.Universe });
            }
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }

        /* more code here*/

        return View(newModel);
    }

【问题讨论】:

  • 我遇到了类似的问题,因为我的路线错误,请确保您在布局中的 css 和 js 导入中使用 @Content,如果您的路线错误,它将点击 /Home/Index/Content/ css 而不是 /Content/css 你会看到错误。当您当前所在的路径对所有文件都正确时,您不会看到任何错误,其他时候从其他位置您会看到错误。只是一个想法。

标签: asp.net-mvc exception http-post dangerous-request


【解决方案1】:

如果这是 ASP.NET MVC 3+,您可以在包含 XML 的模型属性上使用 [AllowHtml] 属性。这将仅禁用此属性的请求验证,而不是整个请求:

public class SimulationModel 
{ 
    [AllowHtml]
    public string StringData { get; set; } 
}

然后:

[HttpPost]
public ActionResult EditSimulation(Guid id, SimulationModel model)
{
    // you could directly use model.StringData here without any 
    // encoding needed

    return View(newModel);
}

如果这是一个在 ASP.NET 4.0 中运行的 ASP.NET MVC 2 应用程序,除了使用 [ValidateInput(false)] 属性装饰您的控制器操作之外,您可能需要在 web.config 中添加以下内容:

<httpRuntime requestValidationMode="2.0"/>

如果它是 ASP.NET MVC 3 应用程序,则无需将此行放入 web.config 即可使用 [ValidateInput(false)] 属性。

【讨论】:

  • 问题仍然存在。第一次我得到异常第二次没问题。
  • 您使用的是哪个版本的 ASP.NET MVC 框架?以及哪个版本的 ASP.NET?您是否从操作签名中删除了任何自定义属性?
  • MVC 3 DotNet 4. 我只留下了 [HttpPost] 属性
  • 您是否使用[AllowHtml] 属性装饰了包含XML 文本的模型属性,如我的答案的第一部分所示?
  • 是的,否则在我使用 Post 方法之前我会得到一个异常。
猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 2018-06-01
相关资源
最近更新 更多