ASP.NET MVC:按 OutputCache 属性中的 HTTP 状态代码进行过滤/变化

发布于 2024-09-07 23:21:36 字数 486 浏览 1 评论 0原文

在我正在编写的 ASP.NET MVC 站点中,我正在构建一个由 HttpModule 路由到的通用错误操作, 遵循本教程。在此操作中,我将返回与应用于 HttpModule 内响应的状态代码相对应的视图(执行此操作后,模块将请求传输到相关操作)。

这一切都很好,只是我想实现缓存。我不想在没有过滤/变化的情况下使用 OutputCache 属性,因为这意味着页面将被缓存一次。我希望为每个可能的状态代码缓存一次页面。

是否可以通过 OutputCacheAttribute 的属性以某种方式进行过滤/变化,以便单独缓存每个 Response.StatusCode?

In the ASP.NET MVC site that I'm writing, I'm building a generic Error Action that is routed to by an HttpModule, following this tutorial. In this Action, I will return a View corresponding to the status code that is applied to the response inside the HttpModule (after doing this, the module transfers the request over to the Action in question).

That's all good, except that I want to implement caching. I don't want to use the OutputCache attribute without filtering/varying, because that would mean that the page would be cached once. I want the page to be cached once for every possible status code.

Is it possible to somehow filter/vary with OutputCacheAttribute's properties, so that each Response.StatusCode is cached separately?

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

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

发布评论

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

评论(1

拥醉 2024-09-14 23:21:36

您当前如何处理到错误操作的路由,例如您可以:

/Errors/404
/Errors/500

所有都指向完全相同的操作,并且将为您处理缓存,因为它们是独立的 url,并且您将 OutputCache 属性一次应用于通用错误操作:

[OutputCache]
public ActionResult DisplayError(int errorCode) {
   return View(errorCode.ToString());
}

这可行吗?

How are you currently handling the routing to your error action, e.g. you could have:

/Errors/404
/Errors/500

All pointing to the exact same action, and the caching will be handled for you because they are independent urls and you apply the OutputCache attribute a single time to the generic error action:

[OutputCache]
public ActionResult DisplayError(int errorCode) {
   return View(errorCode.ToString());
}

Would that work?

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