asp.net-mvc – 将Ninject与Ninject.Web.Api用于Web Api 2在ASP.
发布时间:2020-12-16 03:31:07 所属栏目:asp.Net 来源:网络整理
导读:我正在开发一个Asp.NET MVC项目.我的项目也有web api.我在Visual Studio 3中使用ASP.NET MVC5和Web Api 2.我正在使用ninject进行依赖注入.我知道ninject for web不适用于Web Api 2.所以我尝试使用Ninject for Web Api.请参阅下面的方案. 我使用nuget包管理器
我正在开发一个Asp.NET MVC项目.我的项目也有web api.我在Visual Studio 3中使用ASP.NET MVC5和Web Api 2.我正在使用ninject进行依赖注入.我知道ninject for web不适用于Web Api 2.所以我尝试使用Ninject for Web Api.请参阅下面的方案.
我使用nuget包管理器为web api 2包安装了ninject 然后我使用nuget包管理器安装了Ninject.Web 然后在NinjectWebCommon中,我在RegisterServices中添加了这一行 private static void RegisterServices(IKernel kernel) { System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); kernel.Bind<ICategoryRepo>().To<CategoryRepo>(); } 这是我完整的NinjectWebCommon类注册一个依赖项 [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon),"Start")] [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon),"Stop")] namespace PatheinFashionStore.Web.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; using PatheinFashionStore.Domain.Abstract; using PatheinFashionStore.Domain.Concrete; public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// <summary> /// Starts the application /// </summary> public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// <summary> /// Stops the application. /// </summary> public static void Stop() { bootstrapper.ShutDown(); } /// <summary> /// Creates the kernel that will manage your application. /// </summary> /// <returns>The created kernel.</returns> private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<ICategoryRepo>().To<CategoryRepo>(); } } } 这是我的控制器 public class HomeController : Controller { private ICategoryRepo categoryRepo; public HomeController(ICategoryRepo categoryRepoParam) { this.categoryRepo = categoryRepoParam; } public ActionResult Index() { return View(); } } 然后当我运行我的代码时,它给了我这个错误 这是额外的 但是当我访问apiController时,它正在工作. 这是我的web api控制器 public class TestController : ApiController { private ICategoryRepo categoryRepo; public TestController(ICategoryRepo categoryRepoParam) { this.categoryRepo = categoryRepoParam; } public string Get() { this.categoryRepo.Create(); return "OK"; } } 所以我发现它是为web api工作,但不适用于web项目.我在同一个项目中使用它们. 解决方法
您需要安装
Ninject.MVC5并为MVC设置DependencyResolver以及WebApi的DependencyResolver
// Web Api System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); // MVC System.Web.Mvc.DependencyResolver.SetResolver(new Ninject.Web.Mvc.NinjectDependencyResolver(kernel)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 来自TextBox的ActionLink routeValue
- asp.net-core – 如何在ASP.NET 5中使用JavaScriptSerializ
- ASP.NET MVC默认URL视图
- asp.net – Lucene.Net是否管理访问相同索引的多个线程,一个
- Asp.net利用JQuery AJAX实现无刷新评论思路与代码
- asp.net – 无法访问IIS元数据库
- asp.net-mvc – 隐藏列并在编辑时显示字段或在jqGrid中添加
- asp.net-mvc – 如何在ASP MVC中编码完整的URL字符串
- 一步一步学asp.net Ajax登录设计实现解析
- 使用WPF实现的 喜马拉雅FM 资源下载工具
推荐文章
站长推荐
- asp.net – 如何在visual studio中查看cshtml页面
- asp.net-mvc-3 – 如何配置DbContext以使用Oracl
- 使用带数组的ASP.NET中继器?
- asp.net-mvc – MVC的HTTP缓存 – Last-Modified
- asp.net-mvc-3 – MVC 3使用RenderPage更改视图中
- asp.net-mvc – “安全感知”动作链接?
- asp.net – 单独配置文件为web.config的部分
- asp.net-mvc – ASP.NET MVC – 解决方案布局建议
- iis – 如何查看有多少用户在线?
- 在asp.net core 2.0中使用identityserver4时的无
热点阅读