简体   繁体   English

如何在ASP.NET MVC 4中使用Ninject从Web.config绑定设置?

[英]How to Bind Settings from Web.config using Ninject in ASP.NET MVC 4?

I have a Repository that accepts a Provider which requires a ProviderCredentials object be passed into it. 我有一个Repository ,它接受一个Provider ,它需要将ProviderCredentials对象传递给它。 The ProviderCredentials object is a class, but it's values are only known at run time because they live in the Web.config in the <applicationSettings /> section. ProviderCredentials对象是一个类,但它的值仅在运行时已知,因为它们位于<applicationSettings />部分的Web.config中。

How can I take the values out of the Web.config, build the ProviderCredentials object and inject it into the Provider using Ninject? 如何从Web.config中取出值,构建ProviderCredentials对象并使用Ninject将其注入Provider Not sure if it matters, but the Provider and ProviderCredentials classes live in a separate project than the MVC project in the same solution. 不确定它是否重要,但ProviderProviderCredentials类存在于一个单独的项目中,而不是同一解决方案中的MVC项目。

You may consider binding ProviderCredentials by way of a factory method, forcing the kernel to resolve its reference using your own custom logic: 您可以考虑通过工厂方法绑定ProviderCredentials ,强制内核使用您自己的自定义逻辑解析其引用:

kernel.Bind<ProviderCredentials>()
      .ToMethod(context =>
             new ProviderCredentials(ConfigurationManager.AppSettings["Foo"])
      );

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

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