在C sharp中的ASP.NET中的Http 404错误消息 [英] Http 404 error messages in ASP.NET in C sharp
问题描述
我的网站项目显示了http 404错误消息。我只是在localhost中设计基本级别的链接逻辑。我不知道哪里错了。可以告诉我哪里出错了?我认为它非常简单,不应该显示错误信息。
这是我在C#中的代码。
My website projects shows the http 404 error message. I just design the basic level link logic in my localhost. I don't know where it is wrong. Can tell me where is wrong? I think it is very easy sample and should not show the error message.
This is my code in C#.
protected void CHWebsiteLink_Click(object sender, EventArgs e)
{
LinkButton LnkB = sender as LinkButton;
string strLinkButton = string.Empty;
strLinkButton = LnkB.Text;
#region Link Logic
try
{
if (strLinkButton == "中文")
{
Response.Redirect("~/_form/Default.aspx");
}
else if (strLinkButton == "English")
{
Response.Redirect("~/_form/frmPage.aspc");
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
finally
{
System.Threading.Thread.Sleep(2000);
}
#endregion
}
我尝试过:
我检查了我的aspx文件网址是否正确。
What I have tried:
I have checked my aspx file url that is correct.
推荐答案
你可以允许错误通过抛出或将其添加到控件(如标签)来在网站上显示的消息。但是,如果你调试它,你会很快发现问题。
You can allow the error message to show on the website by throwing it or adding it to a control such as a label. However, if you debug this, you'll find the issue very quickly.
我只推荐这个用于调试目的。您可以使用@RyanDev建议的方法或执行以下操作。 #if DEBUG ... #endif语句用于确保仅在调试模式中显示错误消息。
I'll only recommend this for debugging purposes only. You can use the method suggested by @RyanDev or do the following. The #if DEBUG...#endif statement is to make sure the error message will be displayed only in debug mode.
catch (Exception ex)
{
#if DEBUG
Response.Write(ex.Message);
#endif
}
在生产中,您可以使用错误记录模块和处理程序(ELMAH)记录错误并将请求重定向到通用错误页面。
ELMAH:错误记录模块和处理程序 [ ^ ] [ ^ ]
或者只是将任何错误重定向到一般错误页面,你不想泄漏向对手提供任何信息。
显示自定义错误页面(C#)| Microsoft Docs [ ^ ]
On production, you can use Error Logging Modules And Handlers (ELMAH) to log the errors and redirect the request to the generic error page.
ELMAH: Error Logging Modules And Handlers[^][^]
Or just redirect any error to generic error page, you don't want to leak out any information to the adversary.
Displaying a Custom Error Page (C#) | Microsoft Docs[^]
这篇关于在C sharp中的ASP.NET中的Http 404错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!