这是我在这里的第一篇文章,我已经在这里和网络上的许多其他论坛上搜索了这个问题的解决方案,但没有成功。
我正在尝试为目录访问被拒绝错误 403.14 创建一个自定义错误,以应对有人试图在网站上加载“_assests”目录的情况。我知道我可以通过向每个我希望发生这种情况的目录添加一个 default.aspx 页面来解决这个问题,但我想知道是否有一个站点范围的解决方案类似于 web.config 文件中的标记
<configuration>
<system.web>
<customErrors defaultRedirect="/Errors/GenericError.aspx" mode="RemoteOnly">
<error statusCode="401"
redirect="/Errors/401.aspx"/>
<error statusCode="403"
redirect="/Errors/403.aspx"/>
<error statusCode="404"
redirect="/Errors/404.aspx"/>
<!--
<error statusCode="403.14"
redirect="/"/>
-->
</customErrors>
</system.web>
</configuration>
我在编写 web.config 时遇到错误,我无法使用其中包含小数的 statusCode,因为它不是 Int 数据类型
我在 Server 2008 上有一个 IIS 7。
有什么想法吗?
最佳答案
如果这听起来很困惑,请提前致歉。很高兴澄清。
将以下内容添加到 web.config
中,到目前为止它似乎可以正常工作。不知道为什么,但如果我没有明确告诉 403 错误重定向到自定义 403.aspx
页面,而不是 GenericError.aspx
页面,我收到一个 500 错误。但是,如果我将 404 错误重定向到我的自定义 404.aspx
页面,则 GenericError.aspx
代码就地编写,而不是预期的那样,并且您似乎永远不会被重定向到实际的 404.aspx
页面(请参阅 web.config
的注释部分)。奇怪。
代码:
网络配置文件:
<system.webServer>
<httpErrors existingResponse="Replace" errorMode="Custom">
<remove statusCode="403"/>
<remove statusCode="404"/>
<error statusCode="403" path="/Errors/403.aspx" responseMode="Redirect" />
<!-- <error statusCode="403" path="/Errors/GenericError.aspx" responseMode="Redirect" /> -->
<!-- <error statusCode="404" path="/Errors/GenericError.aspx" responseMode="Redirect" /> -->
<error statusCode="404" path="/Errors/404.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
通用错误.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
var ex = HttpContext.Current.Server.GetLastError();
if (ex is HttpException)
{
var nex = ex as HttpException;
//Label in the Main code displays the Error Code from the Error String
this.customErrorMessageCode.Text += "Error Code" + " " + nex.GetHttpCode().ToString();
//Label in the Main code displays the Error Message from the Error String
this.customErrorMessageLabel.Text += ex.Message.ToString();
//DIV ID in the Main code displays the entire error message
this.customErrorMessage.Visible = true;
switch (nex.GetHttpCode())
{
case 404:
this.customErrorMessageCode.Text += " Page Not Found";
this.customErrorMessageImage.Visible = true;
// do somehting cool
break;
case 403:
this.customErrorMessageCode.Text += " Forbidden Access";
this.customErrorMessageImage.Visible = true;
// do somehting cool
break;
case 500:
this.customErrorMessageCode.Text += " Internal Error";
this.customErrorMessageImage.Visible = true;
// do somehting cool
break;
default:
break;
}
}
else {
this.customErrorMessageLabel.Text += ex.Message + ex.GetType().ToString();
}
}
来源:
关于c# - 为错误 403.14 目录访问被拒绝创建自定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16857983/