绑定前编辑Request.Form

发布于 2024-07-29 01:27:11 字数 314 浏览 2 评论 0原文

有没有办法在操作方法绑定到参数之前编辑Request.Form? 我已经有一个反射调用来启用 Request.Form 的编辑。 我只是找不到可以在绑定发生之前更改它的扩展点。

更新:看起来我正在编辑 Request.Form 但没有意识到。 我通过查看绑定参数进行验证。 这是不正确的,因为当您到达 ActionFilter 时,表单值已被复制/设置到 ValueProvider 中。 我相信这是提取值进行绑定的地方。

因此,问题就变成了在绑定表单值之前对表单值应用一些过滤的最佳方法是什么。 我仍然希望发生绑定。 我只想编辑它用于绑定的值。

Is there a way to edit the Request.Form before the action method binds to the parameters? I already have a reflection call to enable editing of Request.Form. I just can't find a point of extensibilty where I can alter it before binding occurs.

UPDATE: So it looks like I was editing the Request.Form and didn't realize it. I was verifying by looking at the bound parameters. That is incorrect b/c by the time you get to the ActionFilter the form values have already been copied/set to/in the ValueProvider. Which I believe is where the values are pulled for binding.

So the question becomes what is the best way to apply some filtering to the form values before they are bound. I still want the binding to occur. I just want to edit the values it uses to bind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-08-05 01:27:11

创建自定义过滤器并覆盖 OnActionExecuting()

public class CustomActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
}

或者只需在控制器中覆盖 OnActionExecuting()

更新:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var actionName = filterContext.ActionDescriptor.ActionName;

    if(String.Compare(actionName, "Some", true) == 0 && Request.HttpMethod == "POST")
    {  
        var form = filterContext.ActionParameters["form"] as FormCollection;

        form.Add("New", "NewValue");
    }
}

public ActionResult SomeAction(FormCollection form)
{
    ...
}

Create custom filter and override OnActionExecuting():

public class CustomActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
}

Or simply override OnActionExecuting() in your controller

UPDATED:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var actionName = filterContext.ActionDescriptor.ActionName;

    if(String.Compare(actionName, "Some", true) == 0 && Request.HttpMethod == "POST")
    {  
        var form = filterContext.ActionParameters["form"] as FormCollection;

        form.Add("New", "NewValue");
    }
}

public ActionResult SomeAction(FormCollection form)
{
    ...
}
不即不离 2024-08-05 01:27:11

我最终扩展了 DefaultModelBinder 上的 SetProperty 方法,以在继续基本行为之前检查该值。 如果该值是字符串,我将执行过滤。

public class ScrubbingBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        if (value.GetType() == typeof(string))
            value = HtmlScrubber.ScrubHtml(value as string, HtmlScrubber.SimpleFormatTags);
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}

I ended up extending the SetProperty method on the DefaultModelBinder to check the value before proceeding with the base behavior. If the value is a string I perform my filtering.

public class ScrubbingBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        if (value.GetType() == typeof(string))
            value = HtmlScrubber.ScrubHtml(value as string, HtmlScrubber.SimpleFormatTags);
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文