Add a custom response header in ApiController

后端 未结 6 1700
难免孤独
难免孤独 2021-02-05 02:41

Until now, I had a GET method that looked like the following:

protected override async Task GetAll(QueryData query)
{
              


        
6条回答
  •  孤街浪徒
    2021-02-05 03:12

    I have entered comments, here is my complete answer.

    You will need to create a custom filter and apply that to your controller .

    public class CustomHeaderFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
           var count = actionExecutedContext.Request.Properties["Count"];
           actionExecutedContext.Response.Content.Headers.Add("totalHeader", count);
        }
    }
    

    In your Controller

      public class AddressController : ApiController
            {
                public async Task
    Get() { Request.Properties["Count"] = "123"; } }

提交回复
热议问题