在动作方法绑定到参数之前,有没有办法编辑Request.Form?我已经有一个反射调用来启用对Request.Form的编辑。我只是找不到一个可扩展的点,在绑定发生之前我可以修改它。
更新:所以看起来我正在编辑Request.Form,但并没有意识到这一点。我是通过查看绑定参数来验证的。这是不正确的b/c当您到达ActionFilter时,表单值已经被复制/设置到/在ValueProvider中。我认为这就是值被提取以进行绑定的地方。
因此,问题变成了在表单值被绑定之前,什么是对表单值应用过滤的最佳方式。我仍然希望绑定发生。我只想编辑它用来绑定的值。
发布于 2009-08-02 11:44:44
最后,我在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);
}
}发布于 2009-07-29 20:13:13
创建自定义筛选器并覆盖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)
{
...
}https://stackoverflow.com/questions/1202495
复制相似问题