根据Microsoft的文档,对于静态(即HTML)内容,web.config应该读取responseMode="File"每个错误.
目前,我的web.config包括
<httpErrors errorMode="Custom">
<!-- remove statusCodes -->
<error statusCode="404" path="/error/404.html" responseMode="ExecuteURL" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
这将返回正确的自定义错误页面,但返回200 OK状态代码.
当我将"ExecuteURL"更改为"File"时,我的服务器确实返回404,但不显示自定义错误页面.相反,我收到消息"您要查找的资源已被删除,其名称已更改,或暂时不可用".
如何读取web.config,返回静态文件,还有404?
编辑:<customErrors>在得知该标记用于IIS <= 6.0后删除了问题
我无法设置IIS来为我的自定义错误页面提供MVC管道之外的错误.如果我在控制器中抛出一个异常,它就是所有文件,Application_Error事件处理:
protected void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
var routeData = new RouteData();
routeData.Values["Controller"] = "Error";
routeData.Values["Area"] = "";
routeData.Values["Exception"] = error;
if (error is HttpException)
{
switch (((HttpException)error).GetHttpCode())
{
case 401:
routeData.Values["Action"] = "NotAllowed";
break;
case 403:
routeData.Values["Action"] = "NotAllowed";
break;
case 404:
routeData.Values["Action"] = "NotFound";
break;
default:
routeData.Values["Action"] = "ServerError";
break;
}
}
else
{
routeData.Values["Action"] = "ServerError";
}
Response.Clear();
Server.ClearError();
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(((MvcApplication)sender).Context), routeData));
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我浏览到不存在的URL,IIS将处理404错误(或任何其他错误),给我标准的IIS错误消息并完全忽略我的web.config设置:
<system.web>
<customErrors …Run Code Online (Sandbox Code Playgroud)