Wednesday, September 9, 2015

Extend IHttpActionResult to customize API result

Based on business requirement, we can modify Http Response of Web API. The basic idea behind modification of http response is to add some extra/alter information. One scenario might be something like this.

We need to add custom header to response message after completion of certain operation and we want to use this feature in many controller and action.
To implement same, we will inherit IHttpActionResult class to our class to generate custom response from Web API.

Though there are many ways to implement same, here we will observer how we can inherit IHttpActionResult to some class.

Here is sample implementation of ApiResult class which is inherited from IHttpActionResult class. Please note that, ApiResult class is parameterized class and the T is to define content type.

public class ApiResult<T> : IHttpActionResult where T : class
    {
        public ApiResult(HttpRequestMessage request)
        {
            this.StatusCode = HttpStatusCode.OK; ;
            this.HeadersToAdd = new List<MyStringPair>();
            this.Request = request;
        }

        public HttpStatusCode StatusCode { get; set; }
        private List<MyStringPair> HeadersToAdd { get; set; }
        public T Content { get; set; }
        private HttpRequestMessage Request { get; set; }

        public void AddHeaders(string headerKey, string headerValue)
        {
            this.HeadersToAdd.Add(new MyStringPair(headerKey, headerValue));
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = this.Request.CreateResponse<T>(this.StatusCode, this.Content);
            foreach (var hdr in this.HeadersToAdd)
            {
                response.Content.Headers.Add(hdr.key, hdr.value);
            }
            return Task.FromResult(response);
        }


        private class MyStringPair
        {
            public MyStringPair(string key, string value)
            {
                this.key = key;
                this.value = value;
            }
            public string key;
            public string value;
        }
    }

AddHeaders() function will add header of http response message. Here is one simple class

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }

    }

Which I would like to send as content of http response, though this is optional.  Now, here we will write our controller to return http response.

public class ValuesController : ApiController
    {
        public ApiResult<Person> Get()
        {
            var response = new ApiResult<Person>(this.Request);
            response.AddHeaders("key", "value");
            response.Content = new Person { Name ="Sourav" , Surname = "Kayal"};

            return response;
        }

    }


In fiddler, we can see that the header is attached in http response.




No comments:

Post a Comment