'Avoid to many call of DependencyResolver.Current.GetService inside shared layout page
I have a scenario that i need to inject some class into asp.net mvc layout page(using AutoFac).
e.g. :
@{DependencyResolver.Current.GetService<MySampleClass>().MyMethod(); }
this happen tons of time in this view.
Im looking for a best way to avoid this overload on view.
There is a way that i can make Globaly Viewbag or ViewData like this :
public class SampleGlobalDataInjectorAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var model = filterContext.Controller.ViewData.Model as BaseViewModel;
if (model != null)
{
model.MyMethodResult = DependencyResolver.Current.GetService<MySampleClass>().MyMethod();
}
base.OnActionExecuted(filterContext);
}
}
And adding it to class like this :
[SampleGlobalDataInjectorAttribute]
public abstract class BaseController: Controller
{ }
but this happen per action calls and some actio\view doesn't need that injection.
So i guess the only solution left , is to create it one time inside action ? But since Shared
Layout doesn't have unique action how can i make this happen ?
Should create tempData exclusively inside Razor page ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
