如何在 HandleUnauthorizedRequest 中输出默认的 404 页面
我编写了一个自定义 AuthorizeAttribute
,它覆盖了 HandleUnauthorizedRequest
。此覆盖有条件地将响应状态代码设置为 404:
var response = filterContext.HttpContext.Response;
response.StatusCode = 404;
response.ContentType = null;
response.End();
问题是完整响应是:
HTTP/1.1 404 Not Found
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 24 Jan 2011 16:43:08 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Connection: Close
当我想发回默认 404 页面时。例如:
我该怎么做?
I have written a custom AuthorizeAttribute
having an override of HandleUnauthorizedRequest
. This override conditionally sets the response status code to 404 with:
var response = filterContext.HttpContext.Response;
response.StatusCode = 404;
response.ContentType = null;
response.End();
The problem is that the full response is:
HTTP/1.1 404 Not Found
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 24 Jan 2011 16:43:08 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Connection: Close
when I would like to send back the default 404 page. Something like:
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
在 Web.config 中创建自定义 404 处理程序,如下所示:
现在所有 404 错误都转到 My404ErrorPage.aspx(或您选择的任何名称)
你的错误页面),你也可以随意重定向到它。
也许
“Response.WriteError(404);
What about:
Create a custom 404 handler in Web.config as follows:
Now all 404 errors go to My404ErrorPage.aspx (or whatever you choose to name
your error page) and you can also redirect to it at will.
Maybe
"Response.WriteError(404);
我最终做的是设置
过滤器上下文的 Result
属性如下所示:其中“NotFound”是控制器的一个操作,其中包含用我的自定义
AuthorizeAttribute
标记的不同操作。What I ended up doing is setting the
Result
property of the filter context like this:where "NotFound" is an action of the controller containing a different action that was marked with my custom
AuthorizeAttribute
.