c# - Redirect to home/index when 404 -
i'm developing asp.net mvc 5 app .net framework 4.5.1 , c#.
i want redirect home/index when user tries access resource doesn't exist.
yes, there lot of questions how solve problem. read them, solutions don't work me.
now, i'm trying on web.config
:
<system.webserver> <httperrors> <remove statuscode="404" substatuscode="-1" /> <error statuscode="404" path="~/home/index" responsemode="redirect" /> </httperrors> </system.webserver>
but, when try access url, http://host01/views/connectbatch/create.cshtml, default 404 page.
maybe problem in path="~/home/index"
tried path="~/"
, same default 404 page.
any idea?
there lot of thread in stackoverflow case ..working this
<customerrors mode="on" > <error statuscode="404" redirect="/404.shtml" /> </customerrors>
orginal therad
or
you should handle error in code behind (global.asax)
public class mvcapplication : httpapplication { protected void application_endrequest() { if (context.response.statuscode == 404) { response.clear(); var routedata= new routedata(); routedata.datatokens["area"] = "errorarea"; // in case controller in area routedata.values["controller"] = "errors"; routedata.values["action"] = "notfound"; icontroller c = new errorscontroller(); c.execute(new requestcontext(new httpcontextwrapper(context), routedata)); } } }
in error controller this
public sealed class errorscontroller : controller { public actionresult notfound() { actionresult result; object model = request.url.pathandquery; if (!request.isajaxrequest()) result = view(model); else return redirecttoaction("index", "homecontroller"); } }
orginal thread
Comments
Post a Comment