You must Sign In to post a response.
  • Category: .NET

    Help in HttpResponseMessage

    Hi,

    we are migrating our mvc to .net core.
    code is

    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    public HttpResponseMessage methodname(..)
    {
    ..............
    List<List1> li=new List<List1>();
    ...............
    return Request.CreateResponse<List<List1>>(HttpStatusCode.Ok, li) //returning list as well
    }

    but in .net core we don't have CreateResponse definition for HttpRequest.

    From online observed that we can do like below

    return new HttpResponseMessage(HttpStatusCode.OK);

    but HttpResponseMessage not supporting to return list 'li'.

    If any one have any idea suggest me.

    -Thanks..
  • #769409
    Please look at the below article that shows how to return HttpResponseMessage containing a custom value.

    You need to call AddWebApiConventions() in the Startup class. This will wire up the HttpResponseMessageOutputFormatter so that it knows how to serialize the response

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc().AddWebApiConventions();
    }

    [HttpGet]
    [ActionName("methodName")]
    public HttpResponseMessage methodName()
    {

    var resp = new HttpResponseMessage()
    {
    Content = new StringContent("[{\"A\":\"11\"},{\"B\":\"12\"},{\"C\":\"13\"}]]")
    };

    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    return resp;
    }
    }

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net


  • Sign In to post your comments