本文介绍了我怎样才能正确地在ASP.NET MVC处理404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚起步的ASP.NET MVC如此忍受我。我周围中搜索这个网站和各种人,并看到的这几个实现。

I am just getting started on ASP.NET MVC so bear with me. I've searched around this site and various others and have seen a few implementations of this.

编辑:我忘了,更不用说我使用RC2

I forgot to mention I am using RC2

使用URL路由:

routes.MapRoute(
    "Error",
     "{*url}",
     new { controller = "Errors", action = "NotFound" }  // 404s
);

以上似乎照顾(由最初的MVC项目假设默认路由表设置)像这样的请求:/等等/等等/等等/等等

The above seems to take care of requests like this (assuming default route tables setup by initial MVC project): "/blah/blah/blah/blah"

重载HandleUnknownAction()在控制器中:

// 404s - handle here (bad action requested
protected override void HandleUnknownAction(string actionName) {
    ViewData["actionName"] = actionName;
    View("NotFound").ExecuteResult(this.ControllerContext);
}

然而,previous策略不处理到一个坏/未知控制器的请求。例如,我没有一个/ IDoNotExist,如果我要求这一点,我从Web服务器获取通用404页面,而不是我的404,如果我使用路由+覆盖。

However the previous strategies do not handle a request to a Bad/Unknown controller. For example, I do not have a "/IDoNotExist", if I request this I get the generic 404 page from the web server and not my 404 if I use routing + override.

所以最后,我的问题是:有没有办法使用MVC框架的路由或别的东西,赶上这种类型的请求本身

So finally, my question is: Is there any way to catch this type of request using a route or something else in the MVC framework itself?

或者我应该只是默认使用Web.Config中的customErrors作为我404处理器,忘记了这一切?我想如果我去的customErrors我不得不通用的404页存储/浏览次数之外,由于直接访问的Web.Config限制。反正任何最佳做法或指导AP preciated。

OR should I just default to using Web.Config customErrors as my 404 handler and forget all this? I assume if I go with customErrors I'll have to store the generic 404 page outside of /Views due to the Web.Config restrictions on direct access. Anyway any best practices or guidance is appreciated.

推荐答案

在code摘自http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx和作品在ASP.net MVC 1.0以及

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

下面是我如何处理HTTP异常:

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e)
{
   Exception exception = Server.GetLastError();
   // Log the exception.

   ILogger logger = Container.Resolve<ILogger>();
   logger.Error(exception);

   Response.Clear();

   HttpException httpException = exception as HttpException;

   RouteData routeData = new RouteData();
   routeData.Values.Add("controller", "Error");

   if (httpException == null)
   {
       routeData.Values.Add("action", "Index");
   }
   else //It's an Http Exception, Let's handle it.
   {
       switch (httpException.GetHttpCode())
       {
          case 404:
              // Page not found.
              routeData.Values.Add("action", "HttpError404");
              break;
          case 500:
              // Server error.
              routeData.Values.Add("action", "HttpError500");
              break;

           // Here you can handle Views to other error codes.
           // I choose a General error template
           default:
              routeData.Values.Add("action", "General");
              break;
      }
  }

  // Pass exception details to the target error View.
  routeData.Values.Add("error", exception);

  // Clear the error on server.
  Server.ClearError();

  // Avoid IIS7 getting in the middle
  Response.TrySkipIisCustomErrors = true;

  // Call target Controller and pass the routeData.
  IController errorController = new ErrorController();
  errorController.Execute(new RequestContext(
       new HttpContextWrapper(Context), routeData));
}

这篇关于我怎样才能正确地在ASP.NET MVC处理404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 05:20
查看更多