ASP.NET 404 http模块
我使用的是名为 EPiServer 的 CMS 产品。我们需要创建自己的显示 404 错误的方法,而使用 .NET 的标准 customErrors 无法实现该方法。我们编写了一个模块,用于检查 HttpStatusCode
。我们在 EndRequest 方法中执行此操作。
如果状态为 404,我们会向 EPiServer 查询相应的 404 页面,然后将请求传输到该页面。然而,这不会返回 404,即使我执行以下操作,也不会返回正确的状态:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.StatusDescription = "Page not Found";
HttpContext.Current.Server.TransferRequest(newPage);
同样,如果我执行 response.redirect 而不是 TransferRequest
那么它不是正确的 404,因为网址有然后改变了......
这样做的正确方法是什么?
提前致谢 铝
Im using a CMS product called EPiServer. We need to create our own method of displaying 404's which just can't be achieved using .NET's standard customErrors. We've writen a module which we use to check for the HttpStatusCode
. We do this in the EndRequest method.
If the status is 404, we query EPiServer for the appropriate 404 page, and then Transfer the request over to that page. However this doesnt return a 404, and even if I do the following the correct status isnt returned:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.StatusDescription = "Page not Found";
HttpContext.Current.Server.TransferRequest(newPage);
Likewise, if I do a response.redirect instead of a TransferRequest
then its not a proper 404 because the url has then changed...
Whats the right way of doing this?
Thanks in advance
Al
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不是您问题的直接答案,但也可以看看这个开源 404 处理程序: https ://www.coderesort.com/p/epicode/wiki/404Handler
它也可以在 Episervers nuget feed 上找到
Not a direct answer to your question but could also take a look at this open source 404 handler: https://www.coderesort.com/p/epicode/wiki/404Handler
It is also available on episervers nuget feed
您使用的是哪个 IIS 版本?对于 IIS7 或 7.5,您可能需要这样的东西:
Which IIS version are you using? For IIS7 or 7.5 you might need something like this:
您应该在 404 页面使用的模板的代码隐藏中设置状态代码。
如果这是一个纯内容页面,请创建一个新模板或向页面添加状态代码属性,并在后面的代码中添加逻辑,以便在不为 null 或为空时发送适当的标头。
You should set the status code in the codebehind of the template you're using for your 404 page.
If this is a plain content page, either create a new template or add a Status Code property to the page and logic in the code behind to send the appropriate header if this is not null or empty.
尝试在错误页面模板中设置您转移到的页面上的状态代码。我不确定是否有必要使用单独的模块 - 您可以简单地在 Global.asax.cs 中处理 HttpApplication 的 Error 事件。
Try setting the status code on the page that you transfer to - in your error page template. I'm not sure that having a separate module is necessary - you can simply handle the HttpApplication's Error event in Global.asax.cs.
感谢您的回复。
我实际上是通过执行以下操作来完成此工作的:
在 EndRequest 事件处理程序中,我将请求转移到正确的 EPiServer 页面,并在调用中包含一个查询字符串参数,即
app.Context.Server.TransferRequest(pageUrl + "&internal=真的”);
然后在 PostRequestHandlerExecute 事件中,我检查查询字符串参数,如果它存在,那只能是因为它是 404,所以我返回正确的状态:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.StatusDescription = "找不到页面";
就像魅力一样。
谢谢
希格斯
Thanks for your responses.
I actually got this working by doing the following:
In the EndRequest event handler I transferred the request off to the correct EPiServer page, and included a querystring param in the call i.e.
app.Context.Server.TransferRequest(pageUrl + "&internal=true");
Then in the PostRequestHandlerExecute event I check for the querystring param, if it exists it can only be because it's a 404 so I return the correct status:
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.StatusDescription = "Page not Found";
Works like a charm.
Thanks
higgsy