asp.net mvc 4 - Catch 404 Error in MVC4 -
i have problem cacting http error 404.0 - not found. have turned on
<customerrors mode="on" defaultredirect="~/error/general"> <error statuscode="404" redirect="~/error/httperror404" /> <error statuscode="500" redirect="~/error/httperror500" /> </customerrors>
in web.config. problem still exist. have tried solution( never reaches method):
protected void application_error() { var exception = server.getlasterror(); var httpexception = exception httpexception; response.clear(); server.clearerror(); var routedata = new routedata(); routedata.values["controller"] = "errors"; routedata.values["action"] = "general"; routedata.values["exception"] = exception; response.statuscode = 500; if (httpexception != null) { response.statuscode = httpexception.gethttpcode(); switch (response.statuscode) { case 403: routedata.values["action"] = "httperror404"; break; case 404: routedata.values["action"] = "httperror404"; break; } } icontroller errorscontroller = new errorcontroller(); var rc = new requestcontext(new httpcontextwrapper(context), routedata); errorscontroller.execute(rc); }
provided @darin dimitrov
this controller:
public class errorcontroller : controller { public actionresult httperror404(string error) { viewdata["title"] = "sorry, error occurred while processing request. (404)"; viewdata["description"] = error; return view("index"); } public actionresult httperror500(string error) { viewdata["title"] = "sorry, error occurred while processing request. (500)"; viewdata["description"] = error; return view("index"); } public actionresult general(string error) { viewdata["title"] = "sorry, error occurred while processing request."; viewdata["description"] = error; return this.view(); }
okay found solution @alistair-findlay , this website. how web.config looks now:
<system.web> <customerrors mode="on" defaultredirect="~/error/general" redirectmode="responserewrite"> </customerrors> </system.web> <system.webserver> <httperrors errormode="detailed" defaultresponsemode="redirect"> <clear/> <error statuscode="404" path="/error/httperror404"/> </httperrors> </system.webserver
and global.asax.cs:
protected void application_error() { if (context.iscustomerrorenabled) showcustomerrorpage(server.getlasterror()); } private void showcustomerrorpage(exception exception) { var httpexception = exception httpexception ?? new httpexception(500, "internal server error", exception); response.clear(); var routedata = new routedata(); routedata.values.add("controller", "error"); routedata.values.add("fromapperrorevent", true); switch (httpexception.gethttpcode()) { case 403: routedata.values.add("action", "httperror403"); break; case 404: routedata.values.add("action", "httperror404"); break; case 500: routedata.values.add("action", "httperror500"); break; default: routedata.values.add("action", "generalerror"); routedata.values.add("httpstatuscode", httpexception.gethttpcode()); break; } server.clearerror(); icontroller controller = new errorcontroller(); controller.execute(new requestcontext(new httpcontextwrapper(context), routedata)); }
and lastly:
public class errorcontroller : controller { public actionresult httperror403(string error) { viewbag.description = error; return this.view(); }
Comments
Post a Comment