Add a custom response header in ApiController

后端 未结 6 1710
难免孤独
难免孤独 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 02:50

    You can explicitly add custom headers in a method like so:

    [HttpGet]
    [Route("home/students")]
    public HttpResponseMessage GetStudents()
    {
           // Get students from Database
    
           // Create the response
            var response = Request.CreateResponse(HttpStatusCode.OK, studends);
    
            // Set headers for paging
            response.Headers.Add("X-Students-Total-Count", studends.Count());
    
           return response;
    }
    

    For more information read this article: http://www.jerriepelser.com/blog/paging-in-aspnet-webapi-http-headers/

提交回复
热议问题