gpt4 book ai didi

asp.net-mvc-4 - 混合使用MVC和Web API错误处理

转载 作者:行者123 更新时间:2023-12-03 07:54:06 25 4
gpt4 key购买 nike

我们有一个MVC 4 Web应用程序,使用web.config文件来处理自定义错误。

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<remove statusCode="500" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/ApplicationError" />
</httpErrors>
</system.webServer>

所有这些都按预期工作。

现在,我们开始使用 AngularJSWeb API在此项目中实现一些新功能。在 Web API Controller 操作中,我们始终返回HttpResponseMessage来指示调用成功/失败。例如:
return Request.CreateResponse(HttpStatusCode.BadRequest, result);

我们遇到的问题(我认为!)是,最初 MVC错误处理正在拦截 BadRequest结果(合理),因此 HttpResponseMessage结果数据永远不会返回给调用的 AngularJS方法。

在此混合( MVC/Web API)环境中处理错误的最佳方法是什么,以使 Web API HttpResponseMessages不丢失?

谢谢。

最佳答案

我不确定是否找到了最佳的解决方案,但是最后我从Web.config中删除了httpErrors部分,并在以下帖子的帮助下在Global.asax中构建了自己的错误处理程序:

  • StackOverflow
  • PrideParrot

  • Global.asax
    public void Application_Error(Object sender, EventArgs e)
    {
    var httpContext = ((MvcApplication) sender).Context;
    var currentController = "";
    var currentAction = "";
    var currentRouteData =
    RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));

    if (currentRouteData != null)
    {
    if (
    !String.IsNullOrEmpty(
    currentRouteData.Values["controller"]?.ToString()))
    {
    currentController = currentRouteData.Values["controller"].ToString();
    }

    if (!String.IsNullOrEmpty(currentRouteData.Values["action"]?.ToString()))
    {
    currentAction = currentRouteData.Values["action"].ToString();
    }
    }

    var ex = Server.GetLastError();
    var httpEx = ex as HttpException;
    var controller = new ErrorController();
    var routeData = new RouteData();
    var statusCode = httpEx?.GetHttpCode() ?? 500;
    string action;

    switch (statusCode)
    {
    case 400:
    action = "BadRequest";
    break;
    case 403:
    action = "AccessDenied";
    break;
    case 404:
    action = "NotFound";
    break;
    default:
    action = "Index";
    break;
    }

    httpContext.ClearError();
    httpContext.Response.Clear();
    httpContext.Response.StatusCode = statusCode;
    httpContext.Response.TrySkipIisCustomErrors = true;

    if (statusCode >= 500)
    {
    Server.Transfer("/Error/ServerError.html");
    return;
    }

    routeData.Values["controller"] = "Error";
    routeData.Values["action"] = action;
    routeData.Values["statusCode"] = statusCode;

    controller.ViewData.Model = new HandleErrorInfo(ex, currentController,
    currentAction);

    ((IController) controller).Execute(
    new RequestContext(new HttpContextWrapper(httpContext), routeData));
    }

    我的错误 Controller 如下所示:
    [AllowAnonymous]
    public sealed class ErrorController
    : AblController
    {
    public ActionResult Index(int statusCode)
    {
    ViewBag.StatusCode = statusCode;
    return View("Error");
    }


    // HTTP 400 - Bad Request
    public ActionResult BadRequest()
    {
    // Now handled by Global.asax - Application_Error
    // Response.StatusCode = (int) HttpStatusCode.BadRequest;
    // Response.TrySkipIisCustomErrors = true;

    if (Request.IsAjaxRequest())
    {
    return Json(
    new
    {
    error = new ErrorSummary("Bad Request")
    });
    }

    return View();
    }


    // HTTP 403 - Access Denied
    public ActionResult AccessDenied()
    {
    // Now handled by Global.asax - Application_Error
    // Response.StatusCode = (int) HttpStatusCode.Forbidden;
    // Response.TrySkipIisCustomErrors = true;

    if (Request.IsAjaxRequest())
    {
    return Json(
    new
    {
    error = new ErrorSummary("Access Denied")
    });
    }

    return View();
    }


    // HTTP 404 - Not Found
    public ActionResult NotFound()
    {
    // Now handled by Global.asax - Application_Error
    // Response.StatusCode = (int) HttpStatusCode.NotFound;
    // Response.TrySkipIisCustomErrors = true;

    if (Request.IsAjaxRequest())
    {
    return Json(
    new
    {
    error = new ErrorSummary("Not Found")
    });
    }

    return View();
    }
    }

    }

    我还关闭了Web.config中的自定义错误模式
    <customErrors mode="Off" />

    此解决方案需要对 进行更多测试,但是到目前为止,它似乎正在按预期/要求执行。

    关于asp.net-mvc-4 - 混合使用MVC和Web API错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753435/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com