asp.net mvc _ViewStart DI Hooks

时间:2011-01-12 11:58:18

标签: asp.net-mvc razor

有没有什么方法可以直接在_ViewStart类上注入/建立依赖项而不直接调用我的服务定位器?

由于 本

1 个答案:

答案 0 :(得分:1)

我不明白当_ViewStart类(System.Web.Mvc.ViewStartPage)没有保存它们的属性时如何注入设置。

假设您试图避免在_ViewStart中获取设置,可能的解决方案是创建一个自定义ViewStartPage类,该类具有您设置的属性,并从那里调用服务定位器:

/* CustomViewStart.cs */
public class CustomViewStart : System.Web.Mvc.ViewStartPage
{
    public ISettings Settings { get; set; }

    public CustomViewStart()
    {
        // Call service locator here
        this.Settings = blah
    }
}

要使用它,您需要在_ViewStart中设置基类:

@* _ViewStart.cshtml *@
@inherits MyTestApp.CustomViewStart

似乎没有(或者我当然找不到)在Web.config中为此设置基类的方法,就像使用basePageType一样:(