简体   繁体   English

使用 Unity.Mvc5 将服务注入 MVC 视图

[英]Inject Service into MVC View with Unity.Mvc5

I've just started working with Unity Dependency Injection.我刚刚开始使用 Unity 依赖注入。 I've figured out how to use it with Controllers, and with Models.我已经弄清楚如何将它与控制器和模型一起使用。 However I'm not sure how to use it with Views...但是我不确定如何将它与视图一起使用...

What I want to do, is be able to retrieve lookup lists from within the View using a registered Service.我想要做的是能够使用注册的服务从视图中检索查找列表。

Looking at this URL http://blog.simontimms.com/2015/06/09/getting-lookup-data-into-you-view/ it shows how to do it with MVC6.查看这个 URL http://blog.simontimms.com/2015/06/09/getting-lookup-data-into-you-view/它显示了如何使用 MVC6 来做到这一点。 It uses the @inject directive in the view.它在视图中使用@inject 指令。 However I'm stuck with MVC5, which doesn't appear to have this directive.但是我坚持使用 MVC5,它似乎没有这个指令。

Is there some MVC5 alternative to @inject?是否有一些 MVC5 替代@inject?

I'm registering my services and repositories like so...我正在像这样注册我的服务和存储库...

Public Shared Sub RegisterComponents()
    Dim container = New UnityContainer()

    container.RegisterType(Of IMyService, MyService)()
    container.RegisterType(Of IRepository(Of MyModel), MyRepository)()

    DependencyResolver.SetResolver(New UnityDependencyResolver(container))
End Sub

I access the services in my controllers like this...我像这样访问控制器中的服务......

<Dependency>
Public Property MyModelService() As IMyService

All I need to know now is how to inject the services into my razor views.我现在需要知道的是如何将服务注入到我的剃刀视图中。

I've found a relatively neat solution.我找到了一个相对简洁的解决方案。

I just create my own view base class to inherit from WebViewPage (generic, and non-generic), and then put the injected service properties in there.我只是创建我自己的视图基类来继承 WebViewPage(通用和非通用),然后将注入的服务属性放在那里。

Namespace MyNamespace
    Public MustInherit Class MyWebViewPage
        Inherits MyWebViewPage(Of Object)

    End Class

    Public MustInherit Class MyWebViewPage(Of T)
        Inherits System.Web.Mvc.WebViewPage(Of T)

        <Dependency>
        Public Property MyModelService() As MyNamespace.IMyModelService

    End Class

End Namespace

I then add this to the Views/Web.config, like so...然后我将它添加到 Views/Web.config,就像这样......

<pages pageBaseType="MyNamespace.MyWebViewPage"/>

All of the service properties and methods are then automatically available to all my views.然后,所有服务属性和方法都可自动用于我的所有视图。

This should probably have limited usage, but I have run into spots where it is handy.这可能应该使用有限,但我遇到了它很方便的地方。

C# Version using Unity使用 Unity 的 C# 版本

public class InjectedWebViewPage : InjectedWebViewPage<object>
{
}

public class InjectedWebViewPage<T> : WebViewPage<T>
{
    [Dependency] protected ISomeService SomeService { get; set; }
    /*
     * Other Services here...
     */

    public override void Execute()
    {
    }
}

Put the new class anywhere, just reference it in the Views/web.config as @user1751825 states将新类放在任何地方,只需在 Views/web.config 中引用它作为@user1751825 状态

<pages pageBaseType="MyNamespace.InjectedWebViewPage"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM