ASP.NET MVC 404 Error Handling [duplicate]

2018-12-31 15:19发布

问题:

Possible Duplicate:
How can I properly handle 404 in ASP.NET MVC?

I\'ve made the changes outlined at 404 Http error handler in Asp.Net MVC (RC 5) and I\'m still getting the standard 404 error page. Do I need to change something in IIS?

回答1:

Yet another solution.

Add ErrorControllers or static page to with 404 error information.

Modify you web.config (in case of controller).

<system.web>
    <customErrors mode=\"On\" >
       <error statusCode=\"404\" redirect=\"~/Errors/Error404\" />
    </customErrors>
</system.web>

Or in case of static page

<system.web>
    <customErrors mode=\"On\" >
        <error statusCode=\"404\" redirect=\"~/Static404.html\" />
    </customErrors>
</system.web>

This will handle both missed routes and missed actions.



回答2:

I\'ve investigated A LOT on how to properly manage 404s in MVC (specifically MVC3), and this, IMHO is the best solution I\'ve come up with:

In global.asax:

public class MvcApplication : HttpApplication
{
    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            Response.Clear();

            var rd = new RouteData();
            rd.DataTokens[\"area\"] = \"AreaName\"; // In case controller is in another area
            rd.Values[\"controller\"] = \"Errors\";
            rd.Values[\"action\"] = \"NotFound\";

            IController c = new ErrorsController();
            c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
        }
    }
}

ErrorsController:

public sealed class ErrorsController : Controller
{
    public ActionResult NotFound()
    {
        ActionResult result;

        object model = Request.Url.PathAndQuery;

        if (!Request.IsAjaxRequest())
            result = View(model);
        else
            result = PartialView(\"_NotFound\", model);

        return result;
    }
}

Edit:

If you\'re using IoC (e.g. AutoFac), you should create your controller using:

var rc = new RequestContext(new HttpContextWrapper(Context), rd);
var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, \"Errors\");
c.Execute(rc);

Instead of

IController c = new ErrorsController();
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));

(Optional)

Explanation:

There are 6 scenarios that I can think of where an ASP.NET MVC3 apps can generate 404s.

Generated by ASP.NET:

  • Scenario 1: URL does not match a route in the route table.

Generated by ASP.NET MVC:

  • Scenario 2: URL matches a route, but specifies a controller that doesn\'t exist.

  • Scenario 3: URL matches a route, but specifies an action that doesn\'t exist.

Manually generated:

  • Scenario 4: An action returns an HttpNotFoundResult by using the method HttpNotFound().

  • Scenario 5: An action throws an HttpException with the status code 404.

  • Scenario 6: An actions manually modifies the Response.StatusCode property to 404.

Objectives

  • (A) Show a custom 404 error page to the user.

  • (B) Maintain the 404 status code on the client response (specially important for SEO).

  • (C) Send the response directly, without involving a 302 redirection.

Solution Attempt: Custom Errors

<system.web>
    <customErrors mode=\"On\">
        <error statusCode=\"404\" redirect=\"~/Errors/NotFound\"/>
    </customError>
</system.web>

Problems with this solution:

  • Does not comply with objective (A) in scenarios (1), (4), (6).
  • Does not comply with objective (B) automatically. It must be programmed manually.
  • Does not comply with objective (C).

Solution Attempt: HTTP Errors

<system.webServer>
    <httpErrors errorMode=\"Custom\">
        <remove statusCode=\"404\"/>
        <error statusCode=\"404\" path=\"App/Errors/NotFound\" responseMode=\"ExecuteURL\"/>
    </httpErrors>
</system.webServer>

Problems with this solution:

  • Only works on IIS 7+.
  • Does not comply with objective (A) in scenarios (2), (3), (5).
  • Does not comply with objective (B) automatically. It must be programmed manually.

Solution Attempt: HTTP Errors with Replace

<system.webServer>
    <httpErrors errorMode=\"Custom\" existingResponse=\"Replace\">
        <remove statusCode=\"404\"/>
        <error statusCode=\"404\" path=\"App/Errors/NotFound\" responseMode=\"ExecuteURL\"/>
    </httpErrors>
</system.webServer>

Problems with this solution:

  • Only works on IIS 7+.
  • Does not comply with objective (B) automatically. It must be programmed manually.
  • It obscures application level http exceptions. E.g. can\'t use customErrors section, System.Web.Mvc.HandleErrorAttribute, etc. It can\'t only show generic error pages.

Solution Attempt customErrors and HTTP Errors

<system.web>
    <customErrors mode=\"On\">
        <error statusCode=\"404\" redirect=\"~/Errors/NotFound\"/>
    </customError>
</system.web>

and

<system.webServer>
    <httpErrors errorMode=\"Custom\">
        <remove statusCode=\"404\"/>
        <error statusCode=\"404\" path=\"App/Errors/NotFound\" responseMode=\"ExecuteURL\"/>
    </httpErrors>
</system.webServer>

Problems with this solution:

  • Only works on IIS 7+.
  • Does not comply with objective (B) automatically. It must be programmed manually.
  • Does not comply with objective (C) in scenarios (2), (3), (5).

People that have troubled with this before even tried to create their own libraries (see http://aboutcode.net/2011/02/26/handling-not-found-with-asp-net-mvc3.html). But the previous solution seems to cover all the scenarios without the complexity of using an external library.



回答3:

The response from Marco is the BEST solution. I needed to control my error handling, and I mean really CONTROL it. Of course, I have extended the solution a little and created a full error management system that manages everything. I have also read about this solution in other blogs and it seems very acceptable by most of the advanced developers.

Here is the final code that I am using:

protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;
            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values[\"controller\"] = \"ErrorManager\";
            routeData.Values[\"action\"] = \"Fire404Error\";
            routeData.Values[\"exception\"] = exception;
            Response.StatusCode = 500;

            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                    case 404:
                        routeData.Values[\"action\"] = \"Fire404Error\";
                        break;
                }
            }
            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController errormanagerController = new ErrorManagerController();
            HttpContextWrapper wrapper = new HttpContextWrapper(Context);
            var rc = new RequestContext(wrapper, routeData);
            errormanagerController.Execute(rc);
        }
    }

and inside my ErrorManagerController :

        public void Fire404Error(HttpException exception)
    {
        //you can place any other error handling code here
        throw new PageNotFoundException(\"page or resource\");
    }

Now, in my Action, I am throwing a Custom Exception that I have created. And my Controller is inheriting from a custom Controller Based class that I have created. The Custom Base Controller was created to override error handling. Here is my custom Base Controller class:

public class MyBasePageController : Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {
        filterContext.GetType();
        filterContext.ExceptionHandled = true;
        this.View(\"ErrorManager\", filterContext).ExecuteResult(this.ControllerContext);
        base.OnException(filterContext);
    }
}

The \"ErrorManager\" in the above code is just a view that is using a Model based on ExceptionContext

My solution works perfectly and I am able to handle ANY error on my website and display different messages based on ANY exception type.



回答4:

Looks like this is the best way to catch everything.

How can I properly handle 404 in ASP.NET MVC?



回答5:

What I can recomend is to look on FilterAttribute. For example MVC already has HandleErrorAttribute. You can customize it to handle only 404. Reply if you are interesed I will look example.

BTW

Solution(with last route) that you have accepted in previous question does not work in much of the situations. Second solution with HandleUnknownAction will work but require to make this change in each controller or to have single base controller.

My choice is a solution with HandleUnknownAction.



回答6:

In IIS, you can specify a redirect to \"certain\" page based on error code. In you example, you can configure 404 - > Your customized 404 error page.