加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – 在MVC4中捕获404错误

发布时间:2020-12-16 00:05:08 所属栏目:asp.Net 来源:网络整理
导读:我有一个问题,因为HTTP错误404.0 – 未找到.我打开了 customErrors mode="On" defaultRedirect="~/Error/General" error statusCode="404" redirect="~/Error/HttpError404" / error statusCode="500" redirect="~/Error/HttpError500" / /customErrors 在We
我有一个问题,因为HTTP错误404.0 – 未找到.我打开了
<customErrors mode="On" defaultRedirect="~/Error/General">
      <error statusCode="404" redirect="~/Error/HttpError404" />
      <error statusCode="500" redirect="~/Error/HttpError500" />
    </customErrors>

在Web.Config中.但问题仍然存在.我也试过这个解决方案(但它永远不会到达方法):

protected void Application_Error()
    {
      var exception = Server.GetLastError();
      var httpException = exception as HttpException;
      Response.Clear();
      Server.ClearError();
      var routeData = new RouteData();
      routeData.Values["controller"] = "Errors";
      routeData.Values["action"] = "General";
      routeData.Values["exception"] = exception;
      Response.StatusCode = 500;
      if (httpException != null)
      {
        Response.StatusCode = httpException.GetHttpCode();
        switch (Response.StatusCode)
        {
          case 403:
            routeData.Values["action"] = "HttpError404";
            break;
          case 404:
            routeData.Values["action"] = "HttpError404";
            break;
        }
      }

      IController errorsController = new ErrorController();
      var rc = new RequestContext(new HttpContextWrapper(Context),routeData);
      errorsController.Execute(rc);
    }

由@Darin Dimitrov提供

这是控制器:

public class ErrorController : Controller
  {

    public ActionResult HttpError404(string error)
    {
      ViewData["Title"] = "Sorry,an error occurred while processing your request. (404)";
      ViewData["Description"] = error;
      return View("Index");
    }

    public ActionResult HttpError500(string error)
    {
      ViewData["Title"] = "Sorry,an error occurred while processing your request. (500)";
      ViewData["Description"] = error;
      return View("Index");
    }


    public ActionResult General(string error)
    {
      ViewData["Title"] = "Sorry,an error occurred while processing your request.";
      ViewData["Description"] = error;
      return this.View();

    }

解决方法

好的,我找到了一个解决方案,感谢@ alistair-findlay和 this website.
这就是web.config现在的样子:
<system.web>
    <customErrors mode="On" defaultRedirect="~/Error/General" redirectMode="ResponseRewrite"> 
        </customErrors>
 </system.web>
  <system.webServer>
<httpErrors errorMode="Detailed" defaultResponseMode="Redirect">
      <clear/>
      <error statusCode="404" path="/Error/HttpError404"/>
    </httpErrors>

  </system.webServer

这是Global.asax.cs:

protected void Application_Error()
    {

      if (Context.IsCustomErrorEnabled)
        ShowCustomErrorPage(Server.GetLastError());

    }
    private void ShowCustomErrorPage(Exception exception)
    {
      var httpException = exception as HttpException ?? new HttpException(500,"Internal Server Error",exception);

      Response.Clear();
      var routeData = new RouteData();
      routeData.Values.Add("controller","Error");
      routeData.Values.Add("fromAppErrorEvent",true);

      switch (httpException.GetHttpCode())
      {
        case 403:
          routeData.Values.Add("action","HttpError403");
          break;

        case 404:
          routeData.Values.Add("action","HttpError404");
          break;

        case 500:
          routeData.Values.Add("action","HttpError500");
          break;

        default:
          routeData.Values.Add("action","GeneralError");
          routeData.Values.Add("httpStatusCode",httpException.GetHttpCode());
          break;
      }

      Server.ClearError();

      IController controller = new ErrorController();
      controller.Execute(new RequestContext(new HttpContextWrapper(Context),routeData));
    }

最后:

public class ErrorController : Controller
  {

    public ActionResult HttpError403(string error)
    {
      ViewBag.Description = error;
      return this.View();
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读