繁体   English   中英

将基本异常转换为HttpException

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM