如何返回自己的“404自定义页面? [英] How to return 'own' 404 custom page?

查看:355
本文介绍了如何返回自己的“404自定义页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在情况下,如果我的网站上发生的错误,我做到以下几点:

  Server.Transfer的(/ error.aspx);

和该页面有code:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    ...    Response.Status code = 404;
}

如果我在本地主机工作,然后再加上404状态返回的页面,页面显示正确的错误说明。

在我发表了同样的code到互联网所有出错的页面仍与404状态code显示,但不具备的内容。相反,他们有标准的404错误信息:


  

404 - 文件或目录未找到


如果Response.Status code = 404注释掉则提供了正确的页面,但它有200状态code行了。

问:如何返回用户友好的错误页面,在同时拥有404错误状态code

有什么想法,欢迎!非常感谢事先!

P.S。 ASP.NET 4.0


解决方案

 <的customErrors模式=ON的defaultRedirect =〜/错误/ GenericErrorPage.aspx>     <错误状态code =404重定向=〜/错误/ 404.aspx/>< /&的customErrors GT;

<一个href=\"http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.71).aspx\">http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.71).aspx

<一个href=\"http://msdn.microsoft.com/en-us/library/aa479319.aspx\">http://msdn.microsoft.com/en-us/library/aa479319.aspx

In case if error occurred on my web site I do the following:

        Server.Transfer("/error.aspx");

and that page has code:

protected void Page_Load(object sender, EventArgs e)
{
    ...

    Response.StatusCode = 404;
}

If I work on the localhost then together with 404 status returned for the page, page displays 'proper error description'.

Once I published the same code to the internet all pages with errors are still displayed with 404 status code, but the don't have the content. Instead, they have the standard 404 error message:

404 - File or directory not found.

if the line "Response.StatusCode = 404" commented out then the proper page is provided, but it has 200 status code.

Question: how to return user-friendly error page that in the same time has 404 error status code?

Any thoughts are welcome! Thanks a lot in advance!

P.S. ASP.NET 4.0

解决方案

<customErrors mode="On" defaultRedirect="~/Error/GenericErrorPage.aspx">

     <error statusCode="404" redirect="~/Error/404.aspx" />

</customErrors>

http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/aa479319.aspx

这篇关于如何返回自己的“404自定义页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆