如何强制ASP.NET中的HTTP错误?
IDE: 的Visual Studio 2013旗舰版
语言: C#ASP.NET网页(CSS)
问题:嗨!我有一个学校的任务,我正在使用ASP.NET(C#)制作一个网站,我需要添加3个自定义错误。 我知道如何强制一个404错误(找不到页面)和一个500错误(在页面后面的代码中出错),但我需要第三个,我不知道我可以强制什么。如何强制ASP.NET中的HTTP错误?
在我的导航菜单中,我有一个“404page.aspx”的href,但那个不存在于我的解决方案中,所以我得到了404错误。
在菜单中,我也有一个“500error.aspx”的href,并且在后面的代码中有错误的代码(“500error.aspx.cs”),所以我得到了500错误。
Web.Config文件:
<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error.aspx?error=404"/>
<error statusCode="500" redirect="Error.aspx?error=500"/>
<error statusCode="x" redirect="Error.aspx?error=x"/>
</customErrors>
Error.aspx.cs(后面Error.aspx代码)文件:
protected void Page_Load(object sender, EventArgs e)
{
LabelResult.Text = "";
String errorcode = Request.QueryString.Get("error");
if (errorcode.ToString().Equals("404"))
{
LabelResult.Text = "Page contains a 404 error.";
}
if (foutcode.ToString().Equals("500"))
{
LabelResult.Text = "Page contains a 500 error.";
}
}
所以基本上我需要一个第三HTTP错误我可以强制使用C#或ASP.NET。
你要么需要访问您没有权限查看或手动抛出自己的错误代码后面的页面:
throw new HttpException(403, "Access denied");
因此,如果我对一个名为“300error.aspx”的新页面做了一个href,但是对于Page_Load后面的代码,它会强制执行403错误? – Kevin 2014-11-02 22:54:54
理论上是的(尽管我可以称它为'403error.aspx'而不是300!) – DavidG 2014-11-02 22:55:35
将href指向300error.aspx,并将urL代码放在Page_Load上,但它给出了这个错误:“类型为'System'的异常。 Web.HttpException'发生在CeeLearnAndDo.dll中,但未在用户代码中处理“eventho我的错误在我的Web.Config – Kevin 2014-11-02 22:59:27
HTTP 403基本上是告诉你,你没有权限访问页。 – DavidG 2014-11-02 22:38:28
@DavidG我知道403做什么,但我该如何强制它? – Kevin 2014-11-02 22:41:56