Friend asked a question, how to output a custom error page, do not use 302 jump. The current page address cannot be changed.
Also executes some code and so on, generates some error information, facilitates the user to submit the feedback.
500 errors, the MVC framework already has a ready-made workaround:
Filters. ADD (new Handleerrorattribute ());
404 errors Now think of the workaround:
First on the code global.asax:
1 protected voidApplication_Error (Objectsender, EventArgs e)2 {3 varex = Server.GetLastError () asHttpException;4 if(ex = =NULL)5 return;6 7 varHttpStatusCode =Ex. Gethttpcode ();8 9 if(HttpStatusCode = =404)Ten { One varHttpContext = (Sender asmvcapplication). Context; A - httpcontext.clearerror (); - httpContext.Response.Clear (); theHttpContext.Response.StatusCode =404; - ServiceFocus.LogService.AddLog (ex); - -HttpContext.Response.ContentType ="text/html; Charset=utf-8"; + varRoutedata =NewRoutedata (); -routedata.values["Controller"] ="Sys"; +routedata.values["Action"] ="NotFound"; A varRequestContext =NewRequestContext (NewHttpcontextwrapper (HttpContext), routedata); at varController = ControllerBuilder.Current.GetControllerFactory (). Createcontroller (RequestContext,"Sys") asSyscontroller; - //Controller. Viewdata.model=model; -(Controller asIController). Execute (RequestContext); - ControllerBuilder.Current.GetControllerFactory (). Releasecontroller (Controller); - } -}
View Code
Controller code:
1 Public classCompressattribute:actionfilterattribute2 {3 Public Override voidonactionexecuting (actionexecutingcontext filtercontext)4 {5 varacceptencoding = filtercontext.httpcontext.request.headers["accept-encoding"];6 if(!string. IsNullOrEmpty (acceptencoding))7 {8Acceptencoding =acceptencoding.tolower ();9 varResponse =FilterContext.HttpContext.Response;Ten if(Acceptencoding.contains ("gzip")) One { AResponse. Appendheader ("content-encoding","gzip"); -Response. Filter =NewGZipStream (response. Filter, compressionmode.compress); - } the Else if(Acceptencoding.contains ("deflate")) - { -Response. Appendheader ("content-encoding","deflate"); -Response. Filter =NewDeflatestream (response. Filter, compressionmode.compress); + } - } + } A } at [Compress] - Public classSyscontroller:controller - { - // - //GET:/sys/ - in Publicactionresult NotFound () - { to returnView (); + } - Publicactionresult Error () the { * returnView (); $ }Panax Notoginseng}
View Code
Web. config
<system.webServer> "detailed" />
At present, there are a few doubts, no drill: Also hope that users know that can dispel one or two, do not have to Google PA source.
1. If you do not add this line of code, the default output is: text/html; The browser directly outputs the content without parsing.
" text/html; Charset=utf-8 ";
2.iis does not use gzip compression, no matter how large the output 404 error page is, it is not automatically compressed. So use the following substitution method.
[Compress] Public class Syscontroller:controller
Guess:
MVC also does a lot of things after the execute phase of action, such as the 1, 2 points mentioned above. Normal 200 requests perform the default filter phase.
When a 404 request is made, these stages are skipped. Maybe 500 requests are similar.
Just a guess, not yet verified,