gpt4 book ai didi

asp.net - 无法使 ninject 在 ASP.NET MVC4 Web 应用程序中工作

转载 作者:行者123 更新时间:2023-12-02 11:14:08 30 4
gpt4 key购买 nike

我正在学习如何在我的 asp.net MVC 4 Web 应用程序中使用 Ninject。我不确定我的概念是否正确。这就是我所做的。

1) 使用 Manage NuGet 安装 Ninject.MVC3

2)在NinjectWebCommon.cs中添加以下代码(该文件是由Nuget自动添加到App_start文件夹中的)

  private static void RegisterServices(IKernel kernel)
{
**kernel.Bind<IProductRepository>.To<ProductRepository>;**
}

3) Controller 代码

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代码

Public Interface IProductRepository
Function GetProducts() As IQueryable(Of Product)
End Interface

5)ProductRepository代码

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

当我调试时,控件没有转到断点NinjectWebCommon.cs 中的RegistryServices() 而是转到ProductController 中的ProductList()。此时 _rProduct 是什么都没有。你能解释一下发生了什么吗?

谢谢

最佳答案

根据此页https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application由于您使用的是 VB.Net,因此需要执行一些额外的步骤。

NOTE: If you're using VB.NET to develop an MVC3 application then there are few additional steps to make everything work as expected:

  • 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 中命中断点。

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 - 无法使 ninject 在 ASP.NET MVC4 Web 应用程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524946/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com