0

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);
        }
    }

这是发布方法:

   [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);
    }
4

1 回答 1

1

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

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)]属性。

于 2012-08-19T12:01:40.947 回答