How to set custom headers when using IHttpActionResult?

前端 未结 7 1353
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 13:31

In ASP.NET Web API 2, the IHttpActionResult offers a lot of value in simplifying controller code and I\'m reluctant to stop using it, but I\'ve hit a problem.

7条回答
  •  伪装坚强ぢ
    2021-02-01 13:49

    very old question. Probably other answers made sense that time but today you can simply add this line without changing or extending IHttpActionResult. It perfectly adds header in your response. Make sure also to format into RFC 1123 standard as below. Otherwise, although Last-Modified appears in the headers, Client is not able to read it using HttpClient.

        System.Web.HttpContext.Current.Response.Headers.
    Add(Microsoft.Net.Http.Headers.HeaderNames.LastModified, DBdateModified.Value.ToString("r"));
    

提交回复
热议问题