gpt4 book ai didi

asp.net - 为什么我的自定义 404 错误处理程序在部署到 Web 服务器后不起作用

转载 作者:行者123 更新时间:2023-12-01 23:39:56 25 4
gpt4 key购买 nike

我关注了this post并创建了一个全局错误处理程序。我自己添加了处理404错误的功能。但是,当我在本地测试时它工作正常,但一旦部署到 Web 服务器,我的自定义消息就不再显示。相反,默认的丑陋的出现了。

在远程调试中,我可以跟踪执行情况,它确实会执行我的自定义 404 错误操作,但不知何故,IIS 在某个时刻接管了。

在我的 Global.asax.cs 中,我有:

protected void Application_Error()
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;

Response.Clear();
Server.ClearError();

var routeData = new RouteData();
routeData.Values["controller"] = "Error";
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"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}

IController errorController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorController.Execute(rc);
}

然后在我的 ErrorHandler.cs 中,我有:

public ActionResult General(Exception exception)
{
// log error

return Content("General error", "text/html");
}

public ActionResult Http403(Exception exception)
{
return Content("Forbidden", "text/plain");
}

public ActionResult Http404(Exception exception)
{
return Content("Page not found.", "text/plain"); // this displays when tested locally, but not after deployed to web server.
}

}

最佳答案

你是对的,远程 IIS 正在接管你的 404 页面。您需要告诉 IIS 跳过自定义错误设置 Response.TrySkipIisCustomErrors = true;

所以你的代码应该是这样的。

protected void Application_Error()
{
//...
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
//...rest of your code
}

另请查看此链接以获取更多信息 http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

关于asp.net - 为什么我的自定义 404 错误处理程序在部署到 Web 服务器后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com