问题:
我想实现一个sessionAccess类,当尝试访问过期的会话时会引发“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
然后,在家庭控制器中,我实现了这个观点:
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,但现在的问题是,我得到任何异常的异常(null引用,applicationexception等...)
有谁可以告诉我这是为什么? 我原以为我只会在SessionExpiredException上找到这个页面......
为什么还要处理其他所有异常????
出于某种原因,过滤器的内部工作似乎出现故障......
答案 0 :(得分:1)
OnException
的默认实施检查HandleErrorAttribute.ExceptionType Property和return
s,如果没有匹配项。代码来自HandleErrorAttribute.OnException
:
if (!ExceptionType.IsInstanceOfType(exception)) {
return;
}
在覆盖OnException
时,您应该将此检查添加到您的实施中。
如果需要,您可以详细下载并检查整个source of asp.net-mvc 3。