如何从WebAPI中的自定义活页夹调用默认模型绑定? [英] How to call Default Model Binding from custom binder in WebAPI?

查看:116
本文介绍了如何从WebAPI中的自定义活页夹调用默认模型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WebAPI中有一个自定义模型绑定程序,它使用来自 Sytem.Web.Http.ModelBinding命名空间的以下方法,该方法 是用于为Web API创建自定义模型绑定程序的正确命名空间: / p>

I have a custom model binder in WebAPI that uses the following method from the `Sytem.Web.Http.ModelBinding' namespace which is the correct namespace for creating custom model binders for Web API:

public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{

}

我在控制器上有一个 HTTP POST 想要使用此自定义模型活页夹。发布的对象包含大约100个字段。我想更改其中2个。我需要的是发生默认的模型绑定,然后为这两个字段操纵该模型绑定的对象,以便一旦控制器接收到该对象,它就是原始的。

I have a HTTP POST on a controller for which I want to use this custom model binder. The posted object contains roughly 100 fields. I want to change 2 of them. What I need is for default model binding to occur and then manipulate that model bound object for those 2 fields so that once the controller receives the object it is pristine.

问题我似乎无法使用上述模型绑定方法中的默认绑定器对对象进行建模。在 MVC 中有以下内容:

The problem is I can't seem to model bind my object using the default binder from the model binding method above. In MVC there was the following:

base.BindModel(controllerContext,bindingContext);

这种相同的方法在WebAPI中不起作用。也许我要解决这个错误,并且还有另一种方法可以实现我想要的功能,所以请提出建议,如果自定义模型活页夹不是正确的方法。我要阻止的操作是必须在控制器内部操作发布的对象。我可以从技术上在绑定模型之后执行此操作,但是我正在尝试在调用堆栈中更早地执行该操作,以便控制器不必担心这两个字段的自定义操作

This same approach does not work in WebAPI. Maybe I'm going about this wrong and there is another way to accomplish what I want so please suggest if a custom model binder is not the correct approach. What I'm trying to prevent doing is have to manipulate the posted object inside the controller. I could technically do that after it has been model bound, but I'm trying to do that earlier in the call stack so that the controller doesn't need to worry about the custom manipulation of those 2 fields.

如何在自定义模型绑定程序中针对 bindingContext 发起默认模型绑定,这样我就可以

How can I initiate default model binding against the bindingContext in my custom model binder so that I have a fully populated object where then I can just manipulate/massage the last 2 fields I need before returning?

推荐答案

在WebApi中,默认是在其中填充的对象,然后我可以在其中操纵/按摩返回之前需要的最后2个字段?模型资料夹是 CompositeModelBinder ,它包装了所有已注册的模型资料夹。如果要重用它的功能,可以执行以下操作:

In WebApi the 'default' model binder is CompositeModelBinder which wraps all registered model binders. If you want to re-use it's functionality you could do something like:

public class MyModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(MyModel)) return false;

        //this is the default webapi model binder provider
        var provider = new CompositeModelBinderProvider(actionContext.ControllerContext.Configuration.Services.GetModelBinderProviders());
        //the default webapi model binder
        var binder = provider.GetBinder(actionContext.ControllerContext.Configuration, typeof(MyModel));

        //let the default binder do it's thing
        var result = binder.BindModel(actionContext, bindingContext);
        if (result == false) return false;

        //TODO: continue with your own binding logic....
    }
}

这篇关于如何从WebAPI中的自定义活页夹调用默认模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆