如何在 HandleUnauthorizedRequest 中输出默认的 404 页面

发布于 2024-10-14 11:10:49 字数 644 浏览 5 评论 0原文

我编写了一个自定义 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 页面时。例如:


默认 ASP.NET 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:


Screenshot of the default ASP.NET 404 page


How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-10-21 11:10:49

怎么样:
在 Web.config 中创建自定义 404 处理程序,如下所示:

<customErrors mode="On">
<error statusCode="404" redirect="My404ErrorPage.aspx" />
</customErrors>

现在所有 404 错误都转到 My404ErrorPage.aspx(或您选择的任何名称)
你的错误页面),你也可以随意重定向到它。

也许“Response.WriteError(404);

What about:
Create a custom 404 handler in Web.config as follows:

<customErrors mode="On">
<error statusCode="404" redirect="My404ErrorPage.aspx" />
</customErrors>

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);

夏末 2024-10-21 11:10:49

我最终做的是设置 过滤器上下文的 Result 属性如下所示:

var response = filterContext.HttpContext.Response;
response.StatusCode = 404;
var viewData = new ViewDataDictionary();
viewData["Id"] = filterContext.HttpContext.Request.RequestContext.RouteData.Values["id"];
filterContext.Result = new ViewResult { ViewName = "NotFound", ViewData = viewData };
return;

其中“NotFound”是控制器的一个操作,其中包含用我的自定义 AuthorizeAttribute 标记的不同操作。

What I ended up doing is setting the Result property of the filter context like this:

var response = filterContext.HttpContext.Response;
response.StatusCode = 404;
var viewData = new ViewDataDictionary();
viewData["Id"] = filterContext.HttpContext.Request.RequestContext.RouteData.Values["id"];
filterContext.Result = new ViewResult { ViewName = "NotFound", ViewData = viewData };
return;

where "NotFound" is an action of the controller containing a different action that was marked with my custom AuthorizeAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文