简体   繁体   English

为什么我不能在global.asax文件夹中添加自定义模型绑定器?

[英]Why can't I add my custom model binder in my global.asax folder?

I see examples all over the net of people setting up their custom model binders like this: 我看到人们设置他们的自定义模型绑定器的例子,如下所示:

// global.asax
protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(YourModel), new YourBinder());
}

But when I try that, it doesn't compile ( .Binders isn't found). 但是当我尝试它时,它不会编译( .Binders )。 What gives? 是什么赋予了?

It turns out this was just a naming conflict because I had put my custom model binder in a folder/namespace called "ModelBinders". 事实证明,这只是一个命名冲突,因为我将自定义模型绑定器放在名为“ModelBinders” 的文件夹/命名空间中 You can fix this one of two ways: 您可以通过以下两种方式解决此问题:

  1. Rename the namespace/folder to something else, eg CustomModelBinders 将名称空间/文件夹重命名为其他名称,例如CustomModelBinders
  2. Use a fully qualified reference to the ModelBinders like this: 使用对ModelBinders的完全限定引用,如下所示:

    System.Web.Mvc.ModelBinders.Binders.Add( /* ... */ );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM