MVC3 自定义错误页面在开发中工作,而不是在服务器上工作

发布于 2024-12-24 18:08:46 字数 382 浏览 0 评论 0原文

我正在使用SO问题中详细介绍的解决方案。我之前在其他网站上使用过它,效果很好,而且它也适用于我的开发盒。但是,当我发布到我们的 Windows VPS 时,错误会返回标准 IIS 错误页面。

我以前从未管理过网络服务器,并且我不确定需要检查哪些设置才能弄清楚为什么它不返回我设置的自定义错误。我尝试将应用程序的 .net 错误页面设置为关闭和打开,并将默认页面设置为根站点,以及“/”。

我还尝试将错误页面(7.5 中的 iis 下)设置为自定义和详细。

这些似乎都对返回的错误页面没有任何影响。我在这里做错了什么?

I am using the solution detailed in this SO Question. I've used this on other sites before and its worked fine, and it works on my dev box. However, when I publish to a our Windows VPS, errors return the standard IIS error page.

I have never administered a web server before, and I am not sure what settings I need to check to figure out why its not returning the custom errors I have set up. I have tried setting .net error pages for the app to off and on, with the default page set to the root site, as well as just '/'.

I have also tried setting the error pages (under iis in 7.5) to custom and detailed.

None of these seem to have any effect on the error page that is returned. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-12-31 18:08:46

我记得有类似的问题,我将这一行添加到代码中

 protected void Application_Error()
 {
 var exc = Server.GetLastError();
 var httpExc = exc as HttpException;
 Response.Clear();
 Server.ClearError();
 var routeData = new RouteData();
 routeData.Values["controller"] = "Error";
 routeData.Values["action"] = "General";
 routeData.Values["exception"] = exc;
 Response.StatusCode = 500;
 if (httpExc != null)
 {
     Response.StatusCode = httpExc.GetHttpCode();
     routeData.Values["action"] = "Http404";
     routeData.Values["exception"] = httpExc;
 }
 Response.TrySkipIisCustomErrors = true; //this fixed it
 IController errorsController = new WWS_Website.Controllers.ErrorController();
 var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
 errorsController.Execute(rc);

 }

I remember having similar problem and I added this line to the code

 protected void Application_Error()
 {
 var exc = Server.GetLastError();
 var httpExc = exc as HttpException;
 Response.Clear();
 Server.ClearError();
 var routeData = new RouteData();
 routeData.Values["controller"] = "Error";
 routeData.Values["action"] = "General";
 routeData.Values["exception"] = exc;
 Response.StatusCode = 500;
 if (httpExc != null)
 {
     Response.StatusCode = httpExc.GetHttpCode();
     routeData.Values["action"] = "Http404";
     routeData.Values["exception"] = httpExc;
 }
 Response.TrySkipIisCustomErrors = true; //this fixed it
 IController errorsController = new WWS_Website.Controllers.ErrorController();
 var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
 errorsController.Execute(rc);

 }
清欢 2024-12-31 18:08:46

您可以使用 web.config 文件中 system.webServer 部分中的 httpErrors 部分来替换标准 IIS 错误页面。这是对 system.web 中的 customErrors 部分的补充。

<system.webServer>
    ...
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
        <remove statusCode="400" subStatusCode="-1" />
        <error statusCode="400" prefixLanguageFilePath="" 
            path="/errors/400" responseMode="ExecuteURL" />
        <remove statusCode="404" subStatusCode="13" />
        <error statusCode="404" subStatusCode="13" prefixLanguageFilePath=""
            path="/errors/file-upload-too-large" responseMode="Redirect" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" 
            path="/errors/404" responseMode="ExecuteURL" />
        <remove statusCode="403" subStatusCode="-1" />
        <error statusCode="403" prefixLanguageFilePath="" 
            path="/errors/403" responseMode="ExecuteURL" />
    </httpErrors>

You can replace the standard IIS error pages by using the httpErrors section in the system.webServer section in your web.config file. This is in addition to the customErrors section in system.web.

<system.webServer>
    ...
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
        <remove statusCode="400" subStatusCode="-1" />
        <error statusCode="400" prefixLanguageFilePath="" 
            path="/errors/400" responseMode="ExecuteURL" />
        <remove statusCode="404" subStatusCode="13" />
        <error statusCode="404" subStatusCode="13" prefixLanguageFilePath=""
            path="/errors/file-upload-too-large" responseMode="Redirect" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" 
            path="/errors/404" responseMode="ExecuteURL" />
        <remove statusCode="403" subStatusCode="-1" />
        <error statusCode="403" prefixLanguageFilePath="" 
            path="/errors/403" responseMode="ExecuteURL" />
    </httpErrors>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文