MVC Error Handling 1

Source: Internet
Author: User

Example 1.

        /// <summary>        ///Error Handling///404 Processing/// </summary>        protected voidApplication_Error (Objectsender, EventArgs e) {            //How to determine the request            if(Request.httpmethod = ="GET")            {                //Get ErrorHttpException exception = Server.GetLastError () asHttpException; if(Exception! =NULL)                {                    if(Exception. Gethttpcode () = =404)                    {                        //outputs the specified stringResponse.Clear (); //Response.Write ("code:404"); //outputs the content under the specified controllerRoutedata Routedata =NewRoutedata (); ROUTEDATA.VALUES.ADD ("Controller","Viewone"); ROUTEDATA.VALUES.ADD ("Action","Temptwo"); ROUTEDATA.VALUES.ADD ("name","Zhangsan"); IController One=NewViewonecontroller (); One. Execute (NewRequestContext (NewHttpcontextwrapper (Context), routedata)); /** If you do not specify an end output, the automatic processing of the frame (which will empty its output) will be performed. * If the specified end, the status code is 200, not 400, So you need to manually specify the status code*/Response.statuscode=404;                        Response.End ();                    Context.clearerror (); }                }            }        }

Example 2.

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

protected voidApplication_Error (Objectsender, EventArgs e) {Exception Exception=Server.GetLastError ();              Response.Clear (); HttpException HttpException= exception asHttpException; Routedata Routedata=NewRoutedata (); ROUTEDATA.VALUES.ADD ("Controller","Error"); if(HttpException = =NULL) {ROUTEDATA.VALUES.ADD ("Action","Index"); }              Else //It's an Http Exception that 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

namespaceMVC. Controllers { Public classErrorcontroller:controller { PublicActionResult Index (stringerror) {viewdata["Title"] ="WebSite website Internal Error"; viewdata["Description"] =error; returnView ("Index"); }           PublicActionResult HttpError404 (stringerror) {viewdata["Title"] ="HTTP 404-File not found"; viewdata["Description"] =error; returnView ("Index"); }           PublicActionResult HttpError500 (stringerror) {viewdata["Title"] ="HTTP 500-Internal server error"; viewdata["Description"] =error; returnView ("Index"); }           PublicActionResult General (stringerror) {viewdata["Title"] ="HTTP Error occurred"; viewdata["Description"] =error; returnView ("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 to process, IIS throws an error directly, because IIS believes that this suffix name is not what you can do

MVC Error Handling 1

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.