c# - ASP.NET MVC Custom Error Handling Application_Error Global.asax? -


I have some basic code to define errors in my MVC application. Currently I have a error in my project Working methods with a controller called HTTPError404 () , HTTPError500 () , and normal . All those string parameters error accept or use the code given below What is the best / proper way to pass data to the error controller for processing? I would like to have a strong solution as possible.

  Protected Zero application_Error (Object Sender, EventArgs E) {exception exception = Server.GetLastError (); Response.Clear (); Exception as HttpException httpException = HttpException; If (httpException! = Null) {Routed Data Routine = New Root Data (); RouteData.Values.Add ("Admin", "Error"); Switch (httpException.GetHttpCode ()) {case 404: // page path data not found. value. Add ("Action", "HTTPERR 404"); break; Case 500: // server error path data value. Add ("Action", "HTTPAR 500 500"); break; Default: route data value. Add ("verb", "normal"); break; } RouteData.Values.Add ("Error", exception); // clear error on server server Cleaner (); // How to correctly pass the root data to the error controller at this point? }}  

Instead of creating a new route for that, you can only access your Controller / Pass the information through Action and QueryString. For example:

  Secure Zero application_Error (Object Sender, EventArgs E) {exception exception = Server.GetLastError (); Response.Clear (); Exception as HttpException httpException = HttpException; If (httpException! = Null) {string action; Switch (httpException.GetHttpCode ()) {case 404: // page not found action = "HTTPERR 404"; break; Case 500: // server error action = "HTTPP 500 500"; break; Default: verb = "normal"; break; } // clear error on server server Cleaner (); Response.Redirect (String.Format ("~ / Error / {0} /? Message = {1}", Action, Exception Message.)); }  

Then your controller will receive whatever you want:

  // GET: / Error / HTTPRIR 404 Public Actionship HTTPPart 404 (string Message) {Return View (see "Some Views", Messages); }  

There are some tradeoffs with your attitude Be careful with looping in this type of error handle. The second thing is that when you are going to handle a 404 through the ASP.NET pipeline, you will create a session object for all those hits. This can be a problem (performance) for heavily used systems.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -