模型中的ASP.NET MVC访问应用程序属性

时间:2022-11-22 04:13:03

my global.asax looks like this

我的global.asax看起来像这样

Imports Castle.Windsor
Imports Castle.Windsor.Configuration.Interpreters

Public Class MvcApplication
    Inherits System.Web.HttpApplication
    Implements IContainerAccessor

    Private Shared IoC As IWindsorContainer

    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

        ' MapRoute takes the following parameters, in order:
        ' (1) Route name
        ' (2) URL with parameters
        ' (3) Parameter defaults
        routes.MapRoute( _
            "Default", _
            "{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = ""} _
        )

    End Sub

    Sub Application_Start()
        RegisterRoutes(RouteTable.Routes)
        IoC = New WindsorContainer(New XmlInterpreter)
    End Sub

    Public ReadOnly Property Container() As Castle.Windsor.IWindsorContainer Implements Castle.Windsor.IContainerAccessor.Container
        Get
            Return IoC
        End Get
    End Property
End Class

how do i access the Container property from within any Model Class? is there a class i can inherit from that gives me the "Application" property as in the view so i could to this

如何从任何模型类中访问Container属性?是否有一个我可以继承的类,它给了我视图中的“应用程序”属性,所以我可以这样做

IoC = Application.Item("Container")...

IoC = Application.Item(“Container”)......

1 个解决方案

#1


4  

You probably don't want your model objects knowing about your IoC container.

您可能不希望模型对象知道您的IoC容器。

But, for the sake of answering your question, you can access it like this:

但是,为了回答您的问题,您可以像这样访问它:

(C#)

var container = ((IContainerAccessor)MvcApplication).Container;

#1


4  

You probably don't want your model objects knowing about your IoC container.

您可能不希望模型对象知道您的IoC容器。

But, for the sake of answering your question, you can access it like this:

但是,为了回答您的问题,您可以像这样访问它:

(C#)

var container = ((IContainerAccessor)MvcApplication).Container;