問題描述
覆蓋我的控制器,我應該在哪個事件中檢查 cookie? (Overriding my controller, which event should I check for a cookie in?)
in my .net mvc application, I am overriding the Controller class and creating my own that I will inherit from for all my controllers.
Which event should I check for a cookie in?
Does OnActionExecuting make sense?
I tried OnActionExecuting, but can't seem to find the cookie collection?
HttpCookie myCookie = ????????????
‑‑‑‑‑
參考解法
方法 1:
Yes, you can check cookies in OnActionExecuting(). It is very easy:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpCookieCollection cookies = Request.Cookies;
// Check your cookies:
bool yourCookieExistsInRequest = cookies["YourCookie"] != null;
HttpCookie yourCookie = cookies["YourCookie"];
base.OnActionExecuting(filterContext);
}