[英].Net Mvc: How to fire a error for Application_Error() manage them?
我在Global.asax的 Application_Error()
中管理所有應用程序錯誤:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Log.LogException(exception);
Response.Clear();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Erro");
if (httpException == null)
{
routeData.Values.Add("action", "Index");
}
else //It's an Http Exception
{
switch (httpException.GetHttpCode())
{
case 404:
//Page not found
routeData.Values.Add("action", "HttpError404");
break;
case 500:
//Server error
routeData.Values.Add("action", "HttpError500");
break;
// Here you can handle Views to other error codes.
// I choose a General error template
default:
routeData.Values.Add("action", "General");
break;
}
}
//Pass exception details to the target error View.
routeData.Values.Add("error", exception);
//Clear the error on server.
Server.ClearError();
//Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
//Call target Controller and pass the routeData.
IController errorController = new ErroController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
因此,我在我的應用程序中具有一個自定義授權屬性,該屬性可以處理未經授權的請求,並且我想重定向到Application_Error()
處理。
因此,我這樣做:
protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
if (context.HttpContext.Request.IsAuthenticated)
{
throw new HttpException(403, "Forbidden Access.");
}
else
{
base.HandleUnauthorizedRequest(context);
}
}
通過這種方式調用Application_Error()
,但是對我來說直接調用異常似乎很丑,所以存在另一種方式嗎? 你覺得男人呢?
您不應在AuthorizeAttribute內部引發異常,因為這可能導致依賴AuthorizeAttribute進行授權檢查的代碼出現性能問題。 AuthorizeAttribute用於檢查授權是否有效,而不是根據此信息采取的措施。 這就是為什么原始代碼不會直接引發異常的原因-它會將任務委托給HttpUnauthorizedResult類。
相反,您應該創建一個自定義處理程序(類似於HttpUnauthorizedResult )以引發異常。 這將把檢查授權和基於未授權采取行動的邏輯明確地分為兩個不同的類別。
public class HttpForbiddenResult : HttpStatusCodeResult
{
public HttpForbiddenResult()
: this(null)
{
}
// Forbidden is equivalent to HTTP status 403, the status code for forbidden
// access. Other code might intercept this and perform some special logic. For
// example, the FormsAuthenticationModule looks for 401 responses and instead
// redirects the user to the login page.
public HttpForbiddenResult(string statusDescription)
: base(HttpStatusCode.Forbidden, statusDescription)
{
}
}
然后在自定義的AuthorizeAttribute中,只需在HandleUnauthorizedRequest中設置新的處理程序。
protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
if (context.HttpContext.Request.IsAuthenticated)
{
// Returns HTTP 403 - see comment in HttpForbiddenResult.cs.
filterContext.Result = new HttpForbiddenResult("Forbidden Access.");
}
else
{
base.HandleUnauthorizedRequest(context);
}
}
如果您需要執行與拋出HttpException不同的操作,則應將ActionResult子類化並在ExecuteResult方法中實現該操作,或使用繼承ActionResult的內置類之一。
因為默認情況下, Unauthorized
不是錯誤 !!! 只需將此方法添加到global.asax
protected void Application_EndRequest(object sender, EventArgs e) {
if (Context.Response.StatusCode == 401 || Context.Response.StatusCode == 403) {
// this is important, because the 401 is not an error by default!!!
throw new HttpException(401, "You are not authorised");
}
}
您的代碼很好。 默認情況下,如果您調用base.HandleUnauthorizedRequest
它將拋出401
異常,該異常會被表單身份驗證模塊攔截,然后您將被重定向到登錄頁面(這可能不是所需的行為)。 所以你的方法是正確的。
另一種可能性是,如果您不想通過Application_Error
,則直接呈現相應的錯誤視圖:
protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
if (context.HttpContext.Request.IsAuthenticated)
{
context.Result = new ViewResult
{
ViewName = "~/Views/Shared/Forbidden.cshtml"
};
}
else
{
base.HandleUnauthorizedRequest(context);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.