题:

我想实现一个sessionAccess类,当尝试访问过期的 session 时,该类将引发“SessionExpired” -Exception。
我想为SessionExpired显示一个特殊页面,而不是YSOD。

这就是我所拥有的:
在Global.asax.cs中

MvcApplication : System.Web.HttpApplication
{

        // http://stackoverflow.com/questions/183316/asp-net-mvc-handleerror
        public class SessionExpiredErrorHandlerAttribute : HandleErrorAttribute
        {

            public override void OnException(ExceptionContext exceptionContext)
            {
                //Logger.Error(exceptionContext.Exception.Message,exceptionContext.Exception);
                //exceptionContext.ExceptionHandled = true;

                // http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html
                UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
                string messagePageUrl = url.Action("SessionExpired", "Home").ToString();
                System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, true);

                base.OnException(exceptionContext);
            } // End Sub OnException

        } // End Class MyErrorHandlerAttribute


        // http://freshbrewedcode.com/jonathancreamer/2011/11/29/global-handleerrorattribute-in-asp-net-mvc3/
        // <customErrors mode="On" />
        // <customErrors mode="RemoteOnlyy" />
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {

            filters.Add(new HandleErrorAttribute());

            filters.Add(new SessionExpiredErrorHandlerAttribute
            {
                ExceptionType = typeof(WebApplications.SessionAccess.SessionExpiredException),
                View = "@btw WHY is anything I write here ignored ???, and why TF can one only set the view, and not the controller as well @",
                Order = 2

            });


        } // End Sub RegisterGlobalFilters


}

这是我的SessionAccess类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace WebApplications
{


    // http://stackoverflow.com/questions/2950064/detect-when-a-users-session-has-exipred
    public class SessionAccess
    {

        // LoginFailedException
        public class SessionExpiredException : System.Exception
        {
            // The default constructor needs to be defined
            // explicitly now since it would be gone otherwise.

            public SessionExpiredException()
            {
            }

            public SessionExpiredException(string strKey)
                : base("Session \"" + strKey + "\" expired, or was never set.")
            {
            }

        }


        static System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                if (System.Web.HttpContext.Current == null)
                    throw new ApplicationException("No Http Context, No Session to Get!");

                return System.Web.HttpContext.Current.Session;
            }
        }


        public static T Get<T>(string key)
        {
            System.Nullable<bool> SessionExists = (System.Nullable<bool>) Session["__sys_" + key + "_hasBeenSet"];
            if (SessionExists == null)
                throw new SessionExpiredException(key);

            if (Session[key] == null)
                return default(T);
            else
                return (T)Session[key];
        }


        public static void Set<T>(string key, T value)
        {
            Session["__sys_" + key + "_hasBeenSet"] = true;
            Session[key] = value;
        }


    } // End Class SessionAccess


} // End Namespace WebApplications

然后,在家庭 Controller 中,我实现以下 View :
  public ActionResult TestPage(string id)
        {


 /*
        WebApplications.SessionAccess.Set<string>("foo", "test");

        string str = WebApplications.SessionAccess.Get<string>("foo");
        Console.WriteLine(str);

        Session.Clear();
        Session.Abandon();

        str = WebApplications.SessionAccess.Get<string>("foo");
        Console.WriteLine(str);
        */

            throw new Exception("bogus");

            return View();
        }

然后我有一个SessionExpired.cshtml,我将它放入Views\Shared
现在,尽管关闭了自定义错误,我仍然可以获取SessionExpired错误消息。
它对于SessionExpiredException正常工作,但是现在的问题是,对于任何异常(空引用,applicationexception等),我都会得到此异常。

有人可以告诉我为什么吗?
我以为我只会在SessionExpiredException上进入此页面...

为什么还要其他所有异常(exception)????

由于某些原因,过滤器的内部工作似乎出现了故障。

最佳答案

OnException的默认实现检查HandleErrorAttribute.ExceptionType Propertyreturn是否不匹配。来自HandleErrorAttribute.OnException的代码:

        if (!ExceptionType.IsInstanceOfType(exception)) {
            return;
        }

当覆盖OnException时,您也应该将此检查添加到实现中。

如果需要,您可以下载并详细检查整个source of asp.net-mvc 3

08-18 16:33