asp.net mvc _ViewStart DI Hooks
Is there any way I can injec开发者_如何转开发t / build up dependencies on a _ViewStart class without calling my service locator directly?
Thanks Ben
I don't understand how you can inject your settings when the _ViewStart class (System.Web.Mvc.ViewStartPage) doesn't have a property to hold them.
Assuming you're trying to avoid fetching your settings in _ViewStart, a possible solution would be to create a custom ViewStartPage class that has a property for your settings, and call the service locator from within there:
/* CustomViewStart.cs */
public class CustomViewStart : System.Web.Mvc.ViewStartPage
{
public ISettings Settings { get; set; }
public CustomViewStart()
{
// Call service locator here
this.Settings = blah
}
}
To use this, you need to set the base class in your _ViewStart:
@* _ViewStart.cshtml *@
@inherits MyTestApp.CustomViewStart
There doesn't seem to be (or certainly I can't find) a way to set the base class for this in Web.config like you can with basePageType :(
精彩评论