简体   繁体   中英

Custom 403 page works one environment and not another

In my ASP.NET MVC web application, I am trying to send a custom error pages with 404 and 403 responses. I'm able to do this on my local IIS with the following code in Global.asax.cs. No web.config support required.

protected void Application_EndRequest()
{
    if (Context.Response.StatusCode == 404)
    {
        Response.Clear();

        var rd = new RouteData();
        rd.DataTokens["area"] = "";
        rd.Values["controller"] = "Error";
        rd.Values["action"] = "NotFound";

        IController c = new ErrorController();
        c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
    }
    else if (Context.Response.StatusCode == 403)
    {
        Response.Clear();
        var rd = new RouteData();
        rd.DataTokens["area"] = "";
        rd.Values["controller"] = "Error";
        rd.Values["action"] = "Forbidden";

        IController c = new ErrorController();
        c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
    }
}

However, this same code doesn't work when I deploy it to my test environment. In my test environment, I still get the 403 response but I don't see my custom Forbidden view. I just get the default 403 view from IIS. What makes it even more odd is that the 404 view works in both environments. I've spent all day comparing the web.config files and other IIS settings to try and pin down what is different about my test environment but no luck. Any help would be greatly appreciated.

Are you actually forbidden (user permission, no read) on that path on the test server?

Is the test server running the same IIS as your dev machine? Might be some server side config to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM