質問

アクションメソッドがパラメーターにバインドする前にRequest.Formを編集する方法はありますか? Request.Formの編集を有効にするリフレクションコールが既にあります。バインドが発生する前に変更できる拡張ポイントを見つけることができません。

UPDATE:そのため、Request.Formを編集していたのに気づかなかったようです。バインドされたパラメーターを見て検証していました。フォームの値が既にValueProviderにコピー/設定されている/ ActionFilterに到達するまでには、これは正しくありません。私が信じているのは、バインドのために値が取得される場所です。

したがって、質問は、バインドされる前にフォーム値に何らかのフィルタリングを適用するための最良の方法は何かになります。バインディングを引き続き発生させたい。バインドに使用する値を編集したいだけです。

役に立ちましたか?

解決 2

最終的に、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);
    }
}

他のヒント

カスタムフィルターを作成し、 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)
{
    ...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top