[英]Convert base Exception into HttpException
我正在開發ASP.Net應用程序,而我目前正努力處理異常。 我已經設法進行了適當的異常處理,但是我真的不知道如何處理不適合UI的內部錯誤。
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
LogManager.GetCurrentClassLogger().Error(ex);
Response.Clear();
HttpException httpEx = ex as HttpException;
if (httpEx == null)
{
switch(ex.GetType())
{
//Convert ex into httpexception
//with statuscode that depends on the exception type
}
}
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Handler");
routeData.Values.Add("exception", httpEx);
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, "Error");
c.Execute(rc);
}
與其嘗試將異常HttpException
轉換為HttpException
,不如嘗試創建一個以原始異常作為內部異常的新HttpException
:
HttpException httpEx = new HttpException(500, msg, ex);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.