你也可以處理您Global.asax.cs
,然後路線他們在不同的HTTP狀態代碼動態基地Application_Error
所有的錯誤邏輯:
protected void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError().GetBaseException();
var routeData = new RouteData();
if (ex.GetType() == typeof(HttpException))
{
var httpException = (HttpException)ex;
switch (httpException.GetHttpCode())
{
case 401:
routeData.Values.Add("action", "NotAuthorized");
break;
case 403:
routeData.Values.Add("action", "NotAuthorized");
break;
case 404:
routeData.Values.Add("action", "PageNotFound");
break;
default:
routeData.Values.Add("action", "GeneralError");
break;
}
}
else
{
routeData.Values.Add("action", "GeneralError");
}
routeData.Values.Add("controller", "Error");
routeData.Values.Add("error", ex);
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
看來401
不會引發所需的HttpException
所以你要手動處理,要符合邏輯:
protected void Application_EndRequest(object sender, EventArgs e)
{
if (Context.Response.StatusCode == 401)
{
throw new HttpException(401, "You are not authorised");
}
}
是的,你的控制器繼承你的基礎控制器的屬性。
試過了。它不會被要求401.我不知道爲什麼。 – jgauffin 2011-02-03 12:02:17