抛出ASP.NET 404错误 [英] Throwing an ASP.NET 404 error

查看:60
本文介绍了抛出ASP.NET 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是每个人的问题:



如果你要访问http://www.asp.net,你会得到一个漂亮的Default.aspx网页。



如果你去http://www.asp.net/NotFound.aspx,你会得到一个漂亮的ASP.NET 404页面找不到页面。 />


如果你去http://www.asp.net/NotFound,你会得到一个漂亮的IIS 404页面找不到页面。



如果你去http://www.asp.net/NotFound.txt,你会得到一个漂亮的IIS 404页面找不到页面。



如果网页(fake.aspx)将结果显示给经过身份验证的用户,但是您希望显示匿名的404页面,那该怎么办?



您可以为经过身份验证的用户设置Response.StatusCode = 200,并为匿名用户设置Response.StatusCode = 404。



这很有效,除了系统显示IIS 404页面未找到页面而不是未找到的ASP.NET 404页面。对于典型的访客,这不会是一个问题。他们看到404页面未找到并继续前进。但是,注意的人会注意到网页的显示方式有所不同。



以下是问题。



如何获得显示ASP的.ASPX页面。 NET 404页面找不到错误信息而不是IIS 404页面找不到页面?





感谢您的输入

解决方案

将此添加到您的网络配置文件中:





 <   customErrors     mode   =  On    defaultRedirect   = 〜/ ErrorPages / 505.htm >  
< 错误 statusCode = 404 重定向 = 404.htm / >
< 错误 statusCode = 403 重定向 = 403.htm / >
< / customErrors >





在页面加载中方法类型此代码用于测试:

  throw   new 异常(  PageLost); 



它将带您进入错误页面。

在网址中键入错误的页面名称。它会带你到找不到页面的页面。


谁告诉你必须使用带有NotFound的URL?!使用 http://www.asp.net/Any_gibberish_at_all [ ^ ],您将得到相同的结果。这是HTTP服务器和站点的常用设置。此错误404表示未找到。当您托管网站时,您可以为所有常见的HTTP错误案例定义

文件名,并在这些名称下放置一些真实的HTML文件。



请参阅:

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes [ ^ ],

http://en.wikipedia.org/wiki/ERROR_404 [ ^ ]。



现在,如果你自己进行Web开发,这是您有义务了解这一点,配置这些文件名(或询问您的Web托管客户服务),开发适当的错误响应HTML文件并将它们置于这些名称下。不这样做被认为是不专业和草率的。就这么做。







另请参阅我对问题评论的评论Trak4Net:在您的错误页面上,您可以提供指向网站首页,网站地图或两者的链接,但几乎没有任何其他内容。



-SA

Here is a question for everyone:

If you to to http://www.asp.net, you get a pretty Default.aspx web-page.

If you go to http://www.asp.net/NotFound.aspx, you get a pretty ASP.NET 404 page not found page.

If you go to http://www.asp.net/NotFound, you get a pretty IIS 404 page not found page.

If you go to http://www.asp.net/NotFound.txt, you get a pretty IIS 404 page not found page.

What if webpage (fake.aspx) that would display it's results to an authenticated user, but you wanted to display the 404 page not found for the anonymous.

You could set the "Response.StatusCode = 200" for authenticated users and set the "Response.StatusCode = 404" for the anonymous users.

This works great, except the system displays the IIS 404 page not found page not the ASP.NET 404 page not found. For the typical visitor, this won't be a problem. They see a 404 page not found and move on. However, a person who is paying attention would notice there was a difference in how the webpage are displayed.

Here is the question.

How do you get an .ASPX page to show the ASP.NET 404 page not found error message instead of the IIS 404 page not found page?


Thanks for your input

解决方案

Add this to your web config file:



<customErrors mode="On" defaultRedirect="~/ErrorPages/505.htm">
  <error statusCode="404" redirect="404.htm"/>
  <error statusCode="403" redirect="403.htm"/>
</customErrors>



In the page load method type this code for test:

throw new Exception("PageLost");


It will take you to Error page.
In the url type a wrong page name. It will take you to page not found page.


Who told you that you have to use the URL with "NotFound"?! Use http://www.asp.net/Any_gibberish_at_all[^], and you will get the same result. This is a very usual setting for a HTTP server and the site. This error 404 means "not found". When you host the site, you can define the
file names for all usual HTTP error cases and put some real HTML files under these names.

Please see:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes[^],
http://en.wikipedia.org/wiki/ERROR_404[^].

Now, if you do Web development yourself, this is your obligation to be aware of that, configure those file names (or ask you Web hosting customer service), develop appropriate error-response HTML files and put them under these names. Failure to do so is considered as unprofessional and sloppy. Just do it.

[EDIT]

See also my comment to the comment to the question by Trak4Net: on your error pages, you can provide a link to a top page of the site, site map, or both, but hardly anything else.

—SA


这篇关于抛出ASP.NET 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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