Reputation: 286
I want to get a value from the user session and display it in the site.master file. How can I do this so that every view page has this value? Do I have to place ViewData["MyValue"] in every controller action? Is there a global way of doing this in one place so I don't have to have the same code in every controller action?
Upvotes: 1
Views: 758
Reputation: 1038840
You could write an action filter attribute and decorate your controller with it:
public class CustomFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
filterContext.Controller.ViewData["MyValue"] = "some value";
}
}
And then decorate the controller with this attribute:
[CustomFilter]
public class MyController: Controller
{
// actions
}
This will ensure that ViewData["MyValue"]
will be set on all action belonging to this controller.
Upvotes: 3