简体   繁体   English

如何稍后在操作方法中覆盖 [ResponseCache] 属性?

[英]How can I override [ResponseCache] attribute property later in the action method?

I have a [ResponseCache] attribute on an action method with the default cache Location = ResponseCacheLocation.Any.我在默认缓存位置 = ResponseCacheLocation.Any 的操作方法上有一个 [ResponseCache] 属性。 But in few cases only I want to override this property to ResponseCacheLocation.Client.但在少数情况下,我只想将此属性覆盖到 ResponseCacheLocation.Client。

Is there a better way to override the attribute in the action method code than using Response.Headers.Add?有没有比使用 Response.Headers.Add 更好的方法来覆盖操作方法代码中的属性? (not even sure if it would work) (甚至不确定它是否会起作用)

Using DefaultHttpContext in the unit test I can access to GetTypedHeaders, but I don't have access to this method from the real Response in the controller.在单元测试中使用 DefaultHttpContext 我可以访问 GetTypedHeaders,但我无法从 controller 中的真实响应中访问此方法。

I can confirm that the [ResponseCache] attribute can be overridden within the action method in the following way:我可以通过以下方式确认可以在操作方法中覆盖[ResponseCache]属性:

HttpContext.Response.Headers["Cache-Control"] = "private,no-cache, no-store, max-age=0";

However, I don't think there's a better way to handle these kind of "exceptions to the rule".但是,我认为没有更好的方法来处理这种“规则例外”。

暂无
暂无

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

相关问题 如何在单独的 class 中为方法覆盖或添加属性,以便刷新 WCF 服务不会影响我的更改? - How can I override or add an attribute to a method in a separate class so that refreshing a WCF service doesn't blow away my changes? ResponseCache属性不会在客户端缓存数据 - ResponseCache attribute does not cache data on client side 添加 ResponseCache 属性时记录缓存控制警告 - Cache-Control warning getting logged when add ResponseCache attribute 如何在启动文件上配置基于属性的路由,而不是在 asp.net 核心 mvc 中使用 controller 动作上的属性 - How can i configure attribute based routing on startup file instead of using attribute on controller action in asp.net core mvc .netcore override controllerlevel单一操作方法的Authorizeattribute - .netcore override controllerlevel Authorizeattribute on single action method 如何通过验证将 JSON 正文属性映射为 .NET Core Web API 操作方法的动态参数? - How can I map JSON body properties as dynamic parameters of a .NET Core web API action method with validation? 带有 ResponseCache 的 ASP.NET MVC 页面确实返回新内容而不是缓存 - ASP.NET MVC Page with ResponseCache on action DOES return new content instead of cache ASP.Net Core Web API-ResponseCache属性未将“ Cache-Control”标头添加到响应中 - ASP.Net Core Web API - ResponseCache attribute is not adding “Cache-Control” header to the reponse 我可以覆盖ASP.Net Core MVC中单个控制器的默认操作 - Can I override the default action for a single controller in ASP.Net Core MVC 如何使用 DbContext class 中存在的 DbSet&lt;&gt; 属性作为 Controller class 中存在的操作方法的参数? - How to use a DbSet<> property present in DbContext class as a parameter for Action-method present in Controller class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM