如何在 MVC 中清除自定义处理错误属性上的会话



我正在MVC上创建自己的自定义HandleError属性。

public class MVCError : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
         //Supposed to remove session here
    }
}

但是我似乎无法使用会话来删除我网站上的特定会话。这可能吗?还是我需要清除 Global.asax 文件上的会话:

    protected void Application_Error()
    {
        Session.Remove("Check");
        Debug.WriteLine("An error has occurred.");
    }

您可以清除使用HttpContext.Current对象HttpContext.Current.Session.Remove("Check");

public class MVCError : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
             //Supposed to remove session here
             HttpContext.Current.Session.Remove("Check");
        }
    }

最新更新