i have setup web.config
in following way display custom 403 error pages reason page isnt shown - still default error page! custom page shown correctly 404
uses same errorcontroller
web.config
<customerrors mode="on" defaultredirect="~/error"> <error redirect="~/error/notfound" statuscode="404" /> <error redirect="~/error/forbidden" statuscode="403" /> </customerrors>
errorcontroller
public class errorcontroller : basecontroller { public viewresult forbidden() { response.statuscode = 403; //you may want set 200 return view("error403"); } }
attribute generates 403:
[attributeusage(attributetargets.class | attributetargets.method, inherited = true, allowmultiple = true)] public class authorizeemattribute : system.web.mvc.authorizeattribute { protected override void handleunauthorizedrequest(system.web.mvc.authorizationcontext filtercontext) { if (filtercontext.httpcontext.request.isauthenticated) { filtercontext.result = new system.web.mvc.httpstatuscoderesult((int)system.net.httpstatuscode.forbidden); } else { base.handleunauthorizedrequest(filtercontext); } } }
<customerrors mode="on" defaultredirect="~/error/error.html" redirectmode="responserewrite"> <error redirect="~/error/403.aspx" statuscode="403" /> <error redirect="~/error/404.aspx" statuscode="404" /> <error redirect="~/error/500.html" statuscode="500" /> </customerrors>
create page 403.aspx in root directory "error"
<%@ page language="c#" inherits="system.web.mvc.viewpage" %> <!doctype html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>403 not found</title> </head> <body> <% server.clearerror(); response.status = "403 - forbidden: access denied."; response.statuscode = 403; %> <div> <h2>403 - forbidden: access denied.</h2> </div> </body> </html>
Comments
Post a Comment