简体   繁体   English

使用URL路由处理ASP.NET Web窗体中的404错误

[英]Handling 404 error in ASP.NET Web Forms with URL Routing

I'm looking at implementing URL Routing on an ASP.NET Web Forms website and have been looking at the best way to handle 404 errors so for example: 我正在考虑在ASP.NET Web窗体网站上实现URL路由,并且一直在寻找处理404错误的最佳方法,例如:

routes.MapPageRoute("NewsRouteArticle", "news/article/{url}", "~/news.aspx")

The URL is matched against a news article in the database. 该URL与数据库中的新闻文章匹配。

What happens if a news article no longer exists? 如果新闻文章不再存在会怎样? How can a handle this and redirect the user to the Custom Error page? 如何处理此问题并将用户重定向到“自定义错误”页面?

I have been reading about Throw New HttpResponseException(HttpStatusCode.NotFound) but it looks like it is for ASP.NET Web API. 我一直在阅读有关Throw New HttpResponseException(HttpStatusCode.NotFound)但是看起来它是针对ASP.NET Web API的。

I'm also aware of the issue that exists with ASP.NET where 404 Errors actually gives a status code of 302 and I want to try and avoid the issue (see this ). 我也知道ASP.NET存在的问题,其中404错误实际上给出了状态代码302,我想尝试避免该问题(请参阅参考资料 )。

One good reason to use ASP.NET.MVC, I have not done anything in webforms for so long, but I used 使用ASP.NET.MVC的一个很好的理由,我已经很长时间没有在Web表单中做任何事情了,但是我使用了

Server.TransferRequest("/my404page.aspx") Server.TransferRequest( “/ my404page.aspx”)

This stopped the 2 requests and the 302 to 404 problem 这停止了​​2个请求和302至404问题

then returned a 404 然后返回404

   Response.Clear()
    Response.TrySkipIisCustomErrors = True
    Response.StatusCode = 404

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM