我一直在玩ASP.NET MVC 4 beta,我现在看到两种类型的控制器:ApiController和Controller.
我对可以选择特定控制器的情况感到困惑.
例如:如果我想返回一个视图,那么我要使用ApiController还是普通的Controller?我知道WCF Web API现在已与MVC集成.
从现在我们可以使用两个控制器可以有人请指出在哪种情况下去相应的控制器.
目前正在编写API站点(.NET Web Api 2.1)
对于我们以前的API站点,我们使用了Ninject.MVC3包并手动连接了依赖项解析器和范围,并根据建议将我们的逻辑插入到NinjectWebCommon中.
这导致我们的新项目出现胃灼热,无参数构造函数错误.在过去,这是一个死的赠品,我们没有正确地连接Web Api中的依赖性解析器.只有这一次,我们是.它就在那里.
var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
Run Code Online (Sandbox Code Playgroud)
所以我有点失落.在上周,我看到Ninject已更新,WebAPI nuget包已复活,因此我决定尝试实施.
我安装了NuGet包Ninject.Web.WebApi(版本3.2).这似乎不包括应用绑定的NinectWebCommon.cs文件.它只添加了必要的组件.做一些挖掘我偶然发现了这个实现.
https://gist.github.com/odytrice/5842010
(我不清楚Nuget包是否正在为我做这个,这都是冗余代码.如果是这样,那很好,我只需要了解如何使用nuget包添加我们的绑定.)
除了加载模块之外,我在线上实现了这一行,我将三个单独的模块加载为测试.
public static INinjectModule[] Modules
{
//get { return new INinjectModule[] { new MainModule() }; }
get { return new INinjectModule[] { new GlobalModule(), new InventoryModule(), new StoreModule() }; }
}
Run Code Online (Sandbox Code Playgroud)
我已经验证了使用这个新代码设置了依赖项解析器.
_resolver = new NinjectHttpResolver(modules);
GlobalConfiguration.Configuration.DependencyResolver = _resolver;
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.
ExceptionMessage:
An error occurred when trying to create a controller of type 'StoreSearchController'.
Make sure that the controller has a …Run Code Online (Sandbox Code Playgroud)