1

アクション メソッドがパラメーターにバインドされる前に Request.Form を編集する方法はありますか? Request.Form の編集を有効にするためのリフレクション呼び出しが既にあります。バインディングが発生する前に変更できる拡張性のポイントが見つかりません。

更新: Request.Form を編集していて気づかなかったようです。バインドされたパラメーターを見て確認していました。ActionFilter に到達するまでに、フォームの値はすでに ValueProvider にコピー/設定されています。私が信じているのは、バインディングのために値がプルされる場所です。

したがって、問題は、バインドされる前にフォームの値にフィルタリングを適用する最良の方法は何かということになります。私はまだバインディングが発生することを望んでいます。バインドに使用する値を編集したいだけです。

4

2 に答える 2

0

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);
    }
}
于 2009-08-02T11:44:43.823 に答える
0

カスタム フィルターを作成してオーバーライドします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)
{
    ...
}
于 2009-07-29T20:13:12.907 に答える