【问题标题】:Setting the type of an object a custom binder would apply to in a vNext application?设置自定义绑定器将应用于 vNext 应用程序的对象的类型?
【发布时间】:2014-12-12 20:32:57
【问题描述】:

您将如何设置您希望活页夹应用到的对象的类型,例如:

ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());

在 vNext 应用程序中?

我可以看到 ModelBinders.Add 有 3 个重载,

1) IModelBinder
2) ModelBinderDescriptor
3) Type

但我不确定如何将旧代码转换为新代码?基本上我想要这种东西:

    services.AddMvc().Configure<MvcOptions>(options =>
    {
        options.ModelBinders.Add(typeof(Person), new PersonModelBinder()));
    });

谢谢!顺便说一句,I have looked here as well.

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc


    【解决方案1】:

    这似乎确实是 MVC 6 中的一个空白。现在您必须直接在活页夹中编写代码。

     public Task<bool> BindModelAsync(ModelBindingContext bindingContext)
     {      
        if (bindingContext.ModelType == typeof(Person))
        {
            var value = // get the value
            bindingContext.Model = value;
    
            return Task.FromResult(true);
        }
    
        return Task.FromResult(false);
     }
    

    Herehere是在框架中用于实现[FromHeader]的类似代码

    Here 是指向问题跟踪的链接,该链接将类似的重载带回 MVC 5。

    【讨论】:

    • 我试图避免这种情况。感谢您提供问题跟踪链接,我会注意的!
    猜你喜欢
    • 2012-10-13
    • 2011-09-11
    • 2011-05-03
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多