i have implemented exception handling mentioned in below link
how pass error message error view in mvc 5?
it working fine. have requirement handle 404 error.
how can that?
if use below code,
<customerrors mode="on"> <error statuscode="404" redirect="/home/error"></error> </customerrors> it works when 404 error occurs. in case other exception occurs error.cshtml call twice , show same exception two times.
web.config
turn off custom errors in system.web
<system.web> <customerrors mode="off" /> </system.web> configure http errors in system.webserver
<system.webserver> <httperrors errormode="custom" existingresponse="auto"> <clear /> <error statuscode="404" responsemode="executeurl" path="/notfound" /> <error statuscode="500" responsemode="executeurl" path="/error" /> </httperrors> </system.webserver> create simple error controller handle requests errorcontoller.cs
[allowanonymous] public class errorcontroller : controller { // get: error public actionresult notfound() { var statuscode = (int)system.net.httpstatuscode.notfound; response.statuscode = statuscode; response.tryskipiiscustomerrors = true; httpcontext.response.statuscode = statuscode; httpcontext.response.tryskipiiscustomerrors = true; return view(); } public actionresult error() { response.statuscode = (int)system.net.httpstatuscode.internalservererror; response.tryskipiiscustomerrors = true; return view(); } } configure routes routeconfig.cs
public static void registerroutes(routecollection routes) { //...other routes routes.maproute( name: "404-notfound", url: "notfound", defaults: new { controller = "error", action = "notfound" } ); routes.maproute( name: "500-error", url: "error", defaults: new { controller = "error", action = "error" } ); //..other routes //i put catch mapping last route //catch invalid (notfound) routes routes.maproute( name: "notfound", url: "{*url}", defaults: new { controller = "error", action = "notfound" } ); } and make sure have views controller actions
views/shared/notfound.cshtml views/shared/error.cshtml if there additional error want handle can follow pattern , add needed. avoid redirects , maintain original http error status raised.
Comments
Post a Comment