5

这个问题之前在 SO 和 MVC3 的上下文中的其他地方已经被问过,并且有一些关于它与 ASP.NET Core RC1 和 RC2 相关的点点滴滴,但没有一个例子实际上展示了如何在 MVC 6 中以正确的方式做到这一点.

有以下几类

 public abstract class BankAccountTransactionModel {

    public long Id { get; set; }
    public DateTime Date { get; set; }
    public decimal Amount { get; set; }
    public readonly string ModelType;
    public BankAccountTransactionModel(string modelType) {
        this.ModelType = modelType;
    }
 }

 public class BankAccountTransactionModel1 : BankAccountTransactionModel{

    public bool IsPending { get; set; }

    public BankAccountTransactionModel1():
            base(nameof(BankAccountTransactionModel1)) {}
 }    

 public class BankAccountTransactionModel2 : BankAccountTransactionModel{

    public bool IsPending { get; set; }

    public BankAccountTransactionModel2():
            base(nameof(BankAccountTransactionModel2)) {}
 }

在我的控制器中,我有这样的东西

[Route(".../api/[controller]")]  
public class BankAccountTransactionsController : ApiBaseController 
{

 [HttpPost]
 public IActionResult Post(BankAccountTransactionModel model) {

        try {
            if (model == null || !ModelState.IsValid) {
                // failed to bind the model
                return BadRequest(ModelState);
            }

            this.bankAccountTransactionRepository.SaveTransaction(model);

            return this.CreatedAtRoute(ROUTE_NAME_GET_ITEM, new { id = model.Id }, model);
        } catch (Exception e) {

            this.logger.LogError(LoggingEvents.POST_ITEM, e, string.Empty, null);
            return StatusCode(500);
        }
    }

}        

我的客户可以发布 BankAccountTransactionModel1 或 BankAccountTransactionModel2,我想使用自定义模型绑定器来确定要绑定的具体模型,该模型基于抽象基类 BankAccountTransactionModel 上定义的属性 ModelType 中的值。

因此,我做了以下

1) 编写了一个简单的模型绑定器提供程序,用于检查类型是否为 BankAccountTransactionModel。如果是这种情况,则返回 BankAccountTransactionModelBinder 的实例。

public class BankAccountTransactionModelBinderProvider : IModelBinderProvider {

    public IModelBinder GetBinder(ModelBinderProviderContext context) {

        if (context == null) throw new ArgumentNullException(nameof(context));

        if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType) {     

            var type1 = context.Metadata.ModelType;
            var type2 = typeof(BankAccountTransactionModel);

            // some other code here?

            // tried this but not sure what to do with it!
            foreach (var property in context.Metadata.Properties) {
                propertyBinders.Add(property, context.CreateBinder(property));
            }

            if (type1 == type2) {
                return new BankAccountTransactionModelBinder(propertyBinders);
            }
        }

        return null;
    }
}

2) 编写 BankAccountTransactionModel

 public class BankAccountTransactionModelBinder : IModelBinder {


 private readonly IDictionary<ModelMetadata, IModelBinder> _propertyBinders;

 public BankAccountTransactionModelBinder(IDictionary<ModelMetadata, IModelBinder> propertyBinders){
                this._propertyBinders = propertyBinders;
                  }
 public Task BindModelAsync(ModelBindingContext bindingContext) {

        if (bindingContext == null) throw new ArgumentNullException(nameof(bindingContext));            

        // I would like to be able to read the value of the property
        // ModelType like this or in some way...
        // This does not work and typeValue is...
        var typeValue = bindingContext.ValueProvider.GetValue("ModelType");

        // then once I know whether it is a Model1 or Model2 I would like to
        // instantiate one and get the values from the body of the Http  
        // request into the properties of the instance  

        var model = Activator.CreateInstance(type);

        // read the body of the request in some way and set the 
        // properties of model                                                                                                           

        var key = some key? 
        var result = ModelBindingResult.Success(key, model);

        // Job done  
        return Task.FromResult(result);
    }

 }

3)最后我在 Startup.cs 中注册提供程序

public void ConfigureServices(IServiceCollection services)
    {       
        services.AddMvc(options => {
            options.ModelBinderProviders.Insert(0, new BankAccountTransactionModelBinderProvider());
            options.Filters.Add(typeof (SetUserContextAttribute));
        });

整个事情看起来没问题,因为实际上调用了提供者,模型构建器也是如此。但是,我似乎无法在模型绑定器的 BindModelAsync 中编码逻辑。

正如代码中的注释所述,我想要在我的模型绑定器中做的所有事情就是从 http 请求的正文中读取,特别是在我的 JSON 中读取 ModelType 的值。然后在此基础上,我想实例化 BankAccountTransactionModel1 或 BankAccountTransactionModel 并最终通过读取正文中的 JSON 来为该实例的属性分配值。

我知道这只是应该如何完成的粗略近似,但我将非常感谢一些帮助,也许是如何或已经完成的示例。

我在 ModelBinder 中遇到过以下代码行的示例

var typeValue = bindingContext.ValueProvider.GetValue("ModelType");

应该读取值。但是,它在我的模型绑定器中不起作用,并且 typeValue 总是如下所示

typeValue
{}
Culture: {}
FirstValue: null
Length: 0
Values: {}
Results View: Expanding the Results View will enumerate the IEnumerable

我也注意到了

bindingContext.ValueProvider
Count = 2
[0]: {Microsoft.AspNetCore.Mvc.ModelBinding.RouteValueProvider}
[1]: {Microsoft.AspNetCore.Mvc.ModelBinding.QueryStringValueProvider}

这可能意味着我没有机会从身体中读取任何内容。

我是否可能需要混合中的“格式化程序”才能获得所需的结果?

类似的自定义模型绑定器的参考实现是否已经存在于某处,以便我可以简单地使用它,也许使用一些简单的模块?

谢谢你。

4

0 回答 0