How to wire common code from a base controller in ASP.NET MVC

前端 未结 3 1577
陌清茗
陌清茗 2021-01-05 09:23

My ASP.NET MVC application is a small part of a bigger ColdFusion app that is going to be soon replaced completely. I\'m passing some parameters from ColdFusion part through

3条回答
  •  滥情空心
    2021-01-05 09:41

    The better approach would be to implement a custom ActionFilterAttribute and override the OnActionExecuting method to handle the logic and then just decorate your actions with the attribute.

    public class CheckCookieAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Check your cookie and handle the redirect here, otherwise, do nothing
            // You can get to your cookie through the filterContext parameter
        }
    }
    
    public class ActionController : Controller
    {
        [CheckCookie]
        public ActionResult GetFoo()
        {
            return View();
        }
    }
    

    Hope this helps.

提交回复
热议问题