- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用此代码返回对象内容,但我想缓存添加 Cache-Control
header 的响应。
[AllowAnonymous]
[Route("GetPublicContent")]
[HttpGet]
public IHttpActionResult GetPublicContent([FromUri]UpdateContentDto dto)
{
if (dto == null)
return BadRequest();
var content = _contentService.GetPublicContent(dto);
if (content == null)
return BadRequest();
return new Ok(content);
}
就是这样!谢谢!!
最佳答案
创建一个继承自 OkNegotiatedContentResult<T>
的新类:
public class CachedOkResult<T> : OkNegotiatedContentResult<T>
{
public CachedOkResult(T content, TimeSpan howLong, ApiController controller) : base(content, controller)
{
HowLong = howLong;
}
public CachedOkResult(T content, IContentNegotiator contentNegotiator, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
: base(content, contentNegotiator, request, formatters) { }
public TimeSpan HowLong { get; private set; }
public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = await base.ExecuteAsync(cancellationToken);
response.Headers.CacheControl = new CacheControlHeaderValue() {
Public = false,
MaxAge = HowLong
};
return response;
}
}
要在您的 Controller 中使用它,只需返回 CachedOkResult
的一个新实例即可类:
public async Task<IHttpActionResult> GetSomething(string id)
{
var value = await GetAsyncResult(id);
// cache result for 60 seconds
return new CachedOkResult<string>(value, TimeSpan.FromSeconds(60), this);
}
标题将像这样通过电线:
Cache-Control:max-age=60
Content-Length:551
Content-Type:application/json; charset=utf-8
... other headers snipped ...
关于c# - Web API 2.0 IHttpActionResult 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372710/
我见过的大多数 Web API 2.0 方法都返回 IHttpActionResult ,它被定义为“定义异步创建 System.Net.Http.HttpResponseMessage 的命令”的接
我有一个 Web API Controller ,其方法类似于(截断不相关的位): [HttpGet] public IHttpActionResult Order(int orderId) {
我正在尝试在 Web API 2 中创建一个自定义 IHttpActionResult 类型,它将以 HTML 而不是 json 的形式返回内容。我正在努力解决的问题是如何对返回我的新 ActionR
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我目前正在研究Asp.net WebAPI2项目。我正在制作一个用于返回自定义HttpStatusCode进行错误处理的自定义方法。 这是我的代码 public IHttpActionResu
我创建了一个ASP.NET Web API,它将调用Java Web服务并以JSON显示一些结果。当Java Web服务未运行时,我收到以下错误消息: {"Message":"An error has
我正在使用 .NET Core 2 创建一个 API,为使用不同技术开发的许多应用程序提供数据。我目前正在从我的方法返回 IActionresult。我一直在研究返回数据的最佳选择是什么,并看到了一些
我已经确定了这些预定义的响应: Ok - 200 NotFound - 404 Exception - 500 Unauthorized - 401 BadRequest - 400 Conflict
我正在尝试编写一个通用方法以使用我的 Web API 2 返回内部服务器错误... 当我的 Web API 的每个端点发生错误时,我返回一个 InternalServerError(new Excep
我创建了一个基于 ASP.NET Web API 的应用程序并遵循以下教程: http://www.asp.net/web-api/overview/getting-started-with-aspn
我正在做一个演示,并弄清楚如何以与我们创建时相同的方式最好地执行 IHttpActionResults,好的,现在在 Web API 2 中使用 badrequest。 [Route("dem
这个问题在这里已经有了答案: When to return IHttpActionResult vs Object (2 个答案) 关闭 6 年前。 在使用 API 时,我通常将它们写在从 IHtt
我是 ASP.NET webapi 的新手,我找不到返回按 id 查询的对象列表的方法。 这是我的 GET 请求的 Controller 方法。我想返回所有具有通过 url 传递的指定 questio
我正在为我的网站开发 Web API,但遇到了问题。 目前,API 应该返回指定用户的详细信息。 这是我的帐户 Controller : using System; using System.Coll
这样做是可能的,但并不理想:(一个大大简化的例子!) [Serializable] public class MyRecord { public string key {get; set;}
如何返回 HttpError(自己的、格式漂亮的前端错误消息)然后使用方便的 IHttpActionResult? public IHttpActionResult Delete(Guid id) {
我正在尝试使用 Ihttpstatus header 获取此 JSON 响应,该 header 声明代码 201 并将 IHttpActionResult 保留为我的方法返回类型。 我要返回的 JSO
我正在尝试为我们的一些 webapi 调用创建一个测试,但我在访问结果时遇到了困难。在我看到的所有示例中,他们都使用 OkNegotiatedContentResult。问题是在我们的 web api
在使用 ASP.NET Web API 的示例中,我看到两种不同的方法用于将数据返回给调用 jQuery 函数。第一个方法返回 Client 类型的对象,但我不确定第二个方法返回什么。 方法 #1(返
有没有一种方法可以使用新的 IHttpActionResult 接口(interface)返回 HttpStatusCode.NoContent 响应消息? 我目前正在使用 return new Ht
我是一名优秀的程序员,十分优秀!