[英]Convert base Exception into HttpException
I'm developing ASP.Net application and I'm currently struggling with exception handling. 我正在开发ASP.Net应用程序,而我目前正努力处理异常。 I've already managed to do proper exception handling but I don't really know how to handle internal errors that are not suitable for UI. 我已经设法进行了适当的异常处理,但是我真的不知道如何处理不适合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.