开发者

Session Data in site.master

开发者 https://www.devze.com 2022-12-16 08:10 出处:网络
I want to get a val开发者_StackOverflowue 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\

I want to get a val开发者_StackOverflowue 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?


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.


<%= Session["MyValue"] %> in the master page

0

精彩评论

暂无评论...
验证码 换一张
取 消