Calling the Session before any Controller Action is run in MVC

后端 未结 1 1542
失恋的感觉
失恋的感觉 2021-01-27 06:37

I have this authentication check in my global.asax file in the Session_OnStart() call:

if (Session[\"Authenticated\"] == null)
        {
            Response.Red         


        
相关标签:
1条回答
  • 2021-01-27 07:04

    You should create a basecontroller that all your controllers inherit from. then you simply have the logic in one place. i.e.:

    public abstract class BaseController : ControllerBase
    

    you could then use the initialize method in the new BaseContoller to do the common logic. i.e.

    [edit] - changed to OnActionExecuting, rather than Initialize. This isn't the most elegant of places to do it as we're on the cusp of the view being called. however, it's a starting point.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // you should be able to get session stuff here!!
        base.OnActionExecuting(filterContext);
    }
    

    and in each controller:

    public class AnotherNormalController : BaseController
    
    0 讨论(0)
提交回复
热议问题