locked
How do we return generic response in WebAPI2 using IHttpActionResult RRS feed

  • Question

  • User430178104 posted

    How do we return generic response in WebAPI2 by using below example. here instead of string i want to return as a generic object.

        public class CustomActionResult : IHttpActionResult
        {
            HttpStatusCode _httpStatusCode;
            string _value;
            HttpRequestMessage _request;
    
            public CustomActionResult(HttpStatusCode httpStatusCode, string value, HttpRequestMessage request)
            {
                _httpStatusCode = httpStatusCode;
                _value = value;
                _request = request;
            }
            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                var response = new HttpResponseMessage()
                {
                    StatusCode = _httpStatusCode,
                    RequestMessage = _request,
                    Content = new StringContent(_value),
                    
                };
                return Task.FromResult(response);
            }
        }

        public class CustomActionResult : IHttpActionResult<o:p></o:p>

        {<o:p></o:p>

            HttpStatusCode _httpStatusCode;<o:p></o:p>

            string _value;<o:p></o:p>

            HttpRequestMessage _request;<o:p></o:p>

    <o:p> </o:p>

            public CustomActionResult(HttpStatusCode httpStatusCode, string value, HttpRequestMessage request)<o:p></o:p>

            {<o:p></o:p>

                _httpStatusCode = httpStatusCode;<o:p></o:p>

                _value = value;<o:p></o:p>

                _request = request;<o:p></o:p>

            }<o:p></o:p>

            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)<o:p></o:p>

            {<o:p></o:p>

                var response = new HttpResponseMessage()<o:p></o:p>

                {<o:p></o:p>

                    StatusCode = _httpStatusCode,<o:p></o:p>

                    RequestMessage = _request,<o:p></o:p>

                    Content = new StringContent(_value)<o:p></o:p>

                    <o:p></o:p>

                };<o:p></o:p>

                return Task.FromResult(response);<o:p></o:p>

            }<o:p></o:p>

        }<o:p></o:p>

    Monday, May 25, 2015 8:46 AM

Answers

  • User1644755831 posted

    Hello pathipathi,

    You can try something like this.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Formatting;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    
    namespace _2052052.Results
    {
      public class CustomActionResult<T> : IHttpActionResult
      {
        HttpStatusCode _httpStatusCode;
    
        T _value;
    
        HttpRequestMessage _request;
    
        public CustomActionResult(HttpStatusCode httpStatusCode, T value, HttpRequestMessage request)
        {
          _httpStatusCode = httpStatusCode;
          _value = value;
          _request = request;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
          var response = new HttpResponseMessage()
          {
            StatusCode = _httpStatusCode,
            RequestMessage = _request,
            Content = new ObjectContent(typeof(T), _value, new JsonMediaTypeFormatter())
          };
          return Task.FromResult(response);
        }
    
      }
    }

    When creating the new object of customactionresult

    new CustomActionResult<object>(httpStatusCode,object,request); 
    //<..> you can pass desired return type to content in brackets

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 4:39 AM

All replies

  • User1644755831 posted

    Hello pathipathi,

    You can try something like this.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Formatting;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    
    namespace _2052052.Results
    {
      public class CustomActionResult<T> : IHttpActionResult
      {
        HttpStatusCode _httpStatusCode;
    
        T _value;
    
        HttpRequestMessage _request;
    
        public CustomActionResult(HttpStatusCode httpStatusCode, T value, HttpRequestMessage request)
        {
          _httpStatusCode = httpStatusCode;
          _value = value;
          _request = request;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
          var response = new HttpResponseMessage()
          {
            StatusCode = _httpStatusCode,
            RequestMessage = _request,
            Content = new ObjectContent(typeof(T), _value, new JsonMediaTypeFormatter())
          };
          return Task.FromResult(response);
        }
    
      }
    }

    When creating the new object of customactionresult

    new CustomActionResult<object>(httpStatusCode,object,request); 
    //<..> you can pass desired return type to content in brackets

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 4:39 AM
  • User430178104 posted

    Thanks Krunal Parekh.

    Tuesday, May 26, 2015 8:45 AM