Error filter in MVC cannot intercept URL path error Resolution

Source: Internet
Author: User

Server error in "/" application. Unable to find the resource.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) may have been removed, or its name has changed, or is temporarily unavailable. Please check the following URL and make sure it is spelled correctly.

requested URL: /SDF

Version information: Microsoft. NET Framework Version: 4.0.30319; ASP. NET version: 4.0.30319.1

//------------------------------------------------------------------------------------------------------------- -------------------------------

In MVC, there is a filter that catches errors, but its usage is implemented using attribute, and can only be added to the Controller and action, so you can't catch the wrong one.

In fact, all the mistakes in theory must have been generated in the controller, but in 2 cases, they wouldn't have been captured.

1, the page does not exist when the corresponding controller is not found, then no controller is executed, so nature will not catch the wrong

2, in the Iauthorizationfilter error, error capture code in Iexceptionfilter, and Iauthorizationfilter priority is higher than iexceptionfilter, so also can not capture

[C-sharp]View Plaincopy
  1. protected void Application_Error (object sender, EventArgs e)
  2. {
  3. Exception Exception = Server.GetLastError ();
  4. Response.Clear ();
  5. HttpException HttpException = exception as httpexception;
  6. Routedata routedata = new Routedata ();
  7. ROUTEDATA.VALUES.ADD ("Controller", "Error");
  8. if (httpexception = = null)
  9. {
  10. ROUTEDATA.VALUES.ADD ("action", "Index");
  11. }
  12. else //it ' s an Http Exception, let's handle It.
  13. {
  14. switch (Httpexception.gethttpcode ())
  15. {
  16. Case 404:
  17. //Page not found.
  18. ROUTEDATA.VALUES.ADD ("action", "HttpError404");
  19. Break ;
  20. Case :
  21. //Server error.
  22. ROUTEDATA.VALUES.ADD ("action", "HttpError500");
  23. Break ;
  24. //Here's can handle views to other error codes.
  25. //I Choose a general error template
  26. Default:
  27. ROUTEDATA.VALUES.ADD ("action", "General");
  28. Break ;
  29. }
  30. }
  31. //Pass exception details to the target error View.
  32. ROUTEDATA.VALUES.ADD ("Error", exception.  Message);
  33. //Clear the error on server.
  34. Server.ClearError ();
  35. //Call target Controller and pass the Routedata.
  36. IController Errorcontroller = new WEB.  Controllers.errorcontroller ();
  37. Errorcontroller.execute (new RequestContext (
  38. new Httpcontextwrapper (Context), routedata));
  39. }

Put this code in Global.asax, and create a new Controller called Error

[C-sharp]View Plaincopy
  1. Namespace MVC. Controllers
  2. {
  3. public class Errorcontroller:controller
  4. {
  5. Public ActionResult Index (string error)
  6. {
  7. viewdata["Title"] = "WebSite website Internal Error";
  8. viewdata["Description"] = error;
  9. return View ("Index");
  10. }
  11. Public ActionResult HttpError404 (string error)
  12. {
  13. viewdata["Title"] = "HTTP 404-File cannot be found";
  14. viewdata["Description"] = error;
  15. return View ("Index");
  16. }
  17. Public ActionResult HttpError500 (string error)
  18. {
  19. viewdata["Title"] = "HTTP 500-Internal server error";
  20. viewdata["Description"] = error;
  21. return View ("Index");
  22. }
  23. Public ActionResult General (string error)
  24. {
  25. viewdata["Title"] = "HTTP error occurred";
  26. viewdata["Description"] = error;
  27. return View ("Index");
  28. }
  29. }
  30. }

This way, you can catch all the errors.

But in fact, this is not perfect, because if you refer to my first question, do not modify IIS settings under IIS6, run MVC, that when the suffix is not. aspx, the error will not be captured

Because the address entered at this time is not given to the Web site for processing, IIS throws an error directly because IIS believes that the suffix is not what you can do.

Error filter in MVC cannot intercept URL path error Resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.