不能够使asp.net mvc4 Web应用程序ninject工作 [英] not able to make ninject work in asp.net mvc4 web application
问题描述
我正在学习如何在我的asp.net MVC 4 Web应用程序使用Ninject。我不知道我有概念的权利。这就是我所做的。
I am learning how to use Ninject in my asp.net MVC 4 web application. I am not sure I have got the concepts right. This is what I did.
1)使用管理的NuGet安装Ninject.MVC3
1) Installed Ninject.MVC3 using Manage NuGet
2)增加了以下code在NinjectWebCommon.cs(被中的NuGet文件夹App_start自动添加文件)
2) Added the following code in NinjectWebCommon.cs(this file was added automatically by Nuget in App_start folder)
private static void RegisterServices(IKernel kernel)
{
**kernel.Bind<IProductRepository>.To<ProductRepository>;**
}
3)控制器code
3) Controller Code
Public Class ProductController
Inherits System.Web.Mvc.Controller
Private _rProduct As IProductRepository
'
' GET: /Product
Sub ProductController(ProductRepository As IProductRepository)
_rProduct = ProductRepository
End Sub
Function ProductList() As ActionResult
Return View(_rProduct.GetProducts())
End Function
End Class
4)IProductRepository code
4) IProductRepository code
Public Interface IProductRepository
Function GetProducts() As IQueryable(Of Product)
End Interface
5)ProductRepository code
5) ProductRepository code
Public Class ProductRepository
Implements IProductRepository
Public Function GetProducts() As IQueryable(Of Product) Implements IProductRepository.GetProducts
Return (New List(Of Product)() From {
New Product() With {.Name = "Football", .Price = 25},
New Product() With {.Name = "Surf board", .Price = 179},
New Product() With {.Name = "Running shoes", .Price = 95}
}.AsQueryable())
End Function
End Class
当我调试,控制不走的断点
RegistryServices()在NinjectWebCommon.cs而是涉及到产品列表()在ProductController的。此时_rProduct不算什么。能否请您解释我发生了什么事?
When I debug, the control does not go to the breakpoint in RegistryServices() in NinjectWebCommon.cs instead comes to the ProductList() in the ProductController. At this point _rProduct is Nothing. Can you please explain me what is happening?
感谢
推荐答案
据该页面的 https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application 你有一些额外的步骤,因为在执行正在使用VB.Net。
According to this page https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application you have some extra steps to perform because you are using VB.Net.
请注意:如果你使用VB.NET开发一个应用程序MVC3然后还有一些额外的步骤,使预期的一切工作:
NOTE: If you're using VB.NET to develop an MVC3 application then there are few additional steps to make everything work as expected:
- 在App_Start:重命名为NinjectMVC3.cs NinjectMVC3.vb
- 在这个要点提供内容替换NinjectMVC3.vb内容: https://gist.github.com/923618
- In App_Start: rename NinjectMVC3.cs to NinjectMVC3.vb
- Replace contents of NinjectMVC3.vb with contents provided in this gist: https://gist.github.com/923618
编辑:下面是我的工作得到了一个NinjectWebCommon.vb。记的命名空间(我使用VbMvc我的项目名称)。我没有修改Global.asax.vb和断点在RegisterServices打击。
Here's a NinjectWebCommon.vb that I got working. Make note of the Namespace (I'm using "VbMvc" for my project name). I did not modify Global.asax.vb and breakpoints are hit in RegisterServices.
Imports Microsoft.Web.Infrastructure.DynamicModuleHelper
Imports Ninject.Web.Common
Imports Ninject.Web
Imports Ninject
Imports Ninject.Web.Mvc
<Assembly: WebActivator.PreApplicationStartMethod(GetType(VbMvc.App_Start.NinjectWebCommon), "StartNinject")>
<Assembly: WebActivator.ApplicationShutdownMethodAttribute(GetType(VbMvc.App_Start.NinjectWebCommon), "StopNinject")>
Namespace VbMvc.App_Start
Public Module NinjectWebCommon
Private ReadOnly bootstrapper As New Bootstrapper()
''' <summary>
''' Starts the application
''' </summary>
Public Sub StartNinject()
DynamicModuleUtility.RegisterModule(GetType(NinjectHttpModule))
DynamicModuleUtility.RegisterModule(GetType(OnePerRequestHttpModule))
bootstrapper.Initialize(AddressOf CreateKernel)
End Sub
''' <summary>
''' Stops the application.
''' </summary>
Public Sub StopNinject()
bootstrapper.ShutDown()
End Sub
''' <summary>
''' Creates the kernel that will manage your application.
''' </summary>
''' <returns>The created kernel.</returns>
Private Function CreateKernel() As IKernel
Dim kernel = New StandardKernel()
kernel.Bind(Of Func(Of IKernel))().ToMethod(Function(ctx) Function() New Bootstrapper().Kernel)
kernel.Bind(Of IHttpModule)().To(Of HttpApplicationInitializationHttpModule)()
RegisterServices(kernel)
Return kernel
End Function
''' <summary>
''' Load your modules or register your services here!
''' </summary>
''' <param name="kernel">The kernel.</param>
Private Sub RegisterServices(ByVal kernel As IKernel)
''kernel.Load(New Bindings.ServiceBindings(), New Bindings.RepositoryBindings(), New Bindings.PresentationBindings(), New Bindings.CrossCuttingBindings())
End Sub
End Module
End Namespace
这篇关于不能够使asp.net mvc4 Web应用程序ninject工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!