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
- protected void Application_Error (object sender, EventArgs e)
- {
- Exception Exception = Server.GetLastError ();
- 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 :
- //Server error.
- ROUTEDATA.VALUES.ADD ("action", "HttpError500");
- Break ;
- //Here's 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. Message);
- //Clear the error on server.
- Server.ClearError ();
- //Call target Controller and pass the Routedata.
- IController Errorcontroller = new WEB. Controllers.errorcontroller ();
- Errorcontroller.execute (new RequestContext (
- new Httpcontextwrapper (Context), routedata));
- }
Put this code in Global.asax, and create a new Controller called Error
[C-sharp]View Plaincopy
- Namespace MVC. Controllers
- {
- public class Errorcontroller:controller
- {
- Public ActionResult Index (string error)
- {
- viewdata["Title"] = "WebSite website Internal Error";
- viewdata["Description"] = error;
- return View ("Index");
- }
- Public ActionResult HttpError404 (string error)
- {
- viewdata["Title"] = "HTTP 404-File cannot be found";
- viewdata["Description"] = error;
- return View ("Index");
- }
- Public ActionResult HttpError500 (string error)
- {
- viewdata["Title"] = "HTTP 500-Internal server error";
- viewdata["Description"] = error;
- return View ("Index");
- }
- Public ActionResult General (string error)
- {
- viewdata["Title"] = "HTTP error occurred";
- viewdata["Description"] = error;
- return View ("Index");
- }
- }
- }
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