使用Intergrated .net 4.0应用程序池创建了一个新的IIS7网站.
以.aspx结尾的网址会显示自定义404其他任何内容都会显示蓝色服务器错误页面"HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用." (所以与IE无关)
<customErrors redirectMode="ResponseRewrite" mode="On" defaultRedirect="/pages/404.aspx" />
</system.web>
<system.webServer>
<httpErrors >
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
也试过了
<httpErrors existingResponse="PassThrough" />
Run Code Online (Sandbox Code Playgroud)
但这只是导致空洞的回应.
我只找到了一个对执行appcmd来测试自定义http错误处理的有用性的引用,但这里是结果.
C:\Windows\System32\inetsrv>appcmd list config "http://mysite/file.notexist" -section:httpErrors
<system.webServer>
<httpErrors>
<error statusCode="401" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="401.htm" />
<error statusCode="403" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="403.htm" />
<error statusCode="404" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="404.htm" />
<error statusCode="405" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="405.htm" />
<error statusCode="406" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="406.htm" />
<error statusCode="412" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="412.htm" />
<error statusCode="500" prefixLanguageFilePath="%SystemDrive%\inetpub\custer …Run Code Online (Sandbox Code Playgroud) 我正在使用我的ASP.NET MVC 2项目.我创建了异常过滤器,用于捕获当用户无权查看某些操作时发生的未授权访问异常.
[CustomError(typeof(UnauthorizedAccessException), "Error", "UnauthorizedAccess")]
public class MyController : BaseController
{
}
Run Code Online (Sandbox Code Playgroud)
异常被抛出之后,我的过滤器将转移到配置的控制器/动作了下面的方法.
public ActionResult UnauthorizedAccess(ExceptionContext context)
{
Response.StatusCode = CustomHttpStatusCode.UnauthorizedUser;
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
最后,前ASP.NET应用结束该请求时,它将调用下面的方法,其位于Global.ascx用于改变自定义的HTTP状态代码到HTTP状态401(未授权访问).
public void Application_EndRequest(object sender, EventArgs e)
{
if (Response.StatusCode == CustomHttpStatusCode.UnauthorizedUser)
{
Response.StatusCode = 401;
}
}
Run Code Online (Sandbox Code Playgroud)
一切都在我的机器上工作正常(IIS 7.5).但它在我的部署网站上不起作用.它仍然返回纯文本"您没有权限查看该目录或网页." 而不是我的自定义错误页面.
PS.以下配置是本案例的当前web.config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="On"></customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="502" subStatusCode="-1" />
<remove statusCode="501" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="412" subStatusCode="-1" />
<remove statusCode="406" subStatusCode="-1" />
<remove …Run Code Online (Sandbox Code Playgroud)