I know how to redirect pages with Rewrite. But when there is a 404 page how would i display my own custom 404 page (i know i can rewrite it) and say this is in fact a 404 and not just a plain rewritten page?
我知道如何用重写重定向页面。但是当有一个404页面时,我该如何显示我自己的自定义404页面(我知道我可以重写它)并说这实际上是一个404页面而不仅仅是一个简单的重写页面呢?
-edit-
编辑- - - - - -
How i can SET the error code instead of asking the server? if the user goes to /user/invaliduser/ i would like to send a 403 or 404 but my rewrite would send it to a valid page to display user info so i would like to know how to say doesnt exist rather then empty fields in a page.
如何设置错误代码而不是询问服务器?如果用户去/user/invaliduser/我想发送一个403或404,但是我的重写会将它发送到一个有效的页面以显示用户信息,所以我想知道如何说不存在,而不是在页面中空字段。
2 个解决方案
#1
2
To be more flexible then strict 404.html follow my example. In Global.asax.cs override:
比严格的404更灵活。html以我为榜样。在Global.asax。cs覆盖:
public void Application_Error(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsLocal) return;
Exception ex = Server.GetLastError();
HttpException httpEx = ex as HttpException;
string errorUrl;
if (httpEx != null && httpEx.GetHttpCode() == 403)
errorUrl = "~/error/403.aspx";
else if (httpEx != null && httpEx.GetHttpCode() == 404)
errorUrl = "~/error/404.aspx";
else
errorUrl = "~/error/500.aspx";
Server.Transfer(errorUrl);
}
For example my 404.aspx contains form to send feedback, 403.aspx - exposes more interesting items for user, and 500 sends alert to Admin.
例如我的404。aspx包含发送反馈的表单,403。aspx -为用户公开更有趣的项目,500发送警报到管理员。
#2
1
U can do a 404.html and redirect the 404 error in the web.config file to your own file
你可以做404。并重定向网页中的404错误。配置文件到您自己的文件
<error statusCode="404" redirect="YOURFILE.htm" />
#1
2
To be more flexible then strict 404.html follow my example. In Global.asax.cs override:
比严格的404更灵活。html以我为榜样。在Global.asax。cs覆盖:
public void Application_Error(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsLocal) return;
Exception ex = Server.GetLastError();
HttpException httpEx = ex as HttpException;
string errorUrl;
if (httpEx != null && httpEx.GetHttpCode() == 403)
errorUrl = "~/error/403.aspx";
else if (httpEx != null && httpEx.GetHttpCode() == 404)
errorUrl = "~/error/404.aspx";
else
errorUrl = "~/error/500.aspx";
Server.Transfer(errorUrl);
}
For example my 404.aspx contains form to send feedback, 403.aspx - exposes more interesting items for user, and 500 sends alert to Admin.
例如我的404。aspx包含发送反馈的表单,403。aspx -为用户公开更有趣的项目,500发送警报到管理员。
#2
1
U can do a 404.html and redirect the 404 error in the web.config file to your own file
你可以做404。并重定向网页中的404错误。配置文件到您自己的文件
<error statusCode="404" redirect="YOURFILE.htm" />