且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

抛出ASP.NET 404错误

更新时间:2023-02-15 20:52:34

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





 <   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