sipho February 2016

Sending a List or Array to Web Api

I am creating an api method to add Result from a Textfile.. My app reads data from text file and populate a generic list of C# Structure call ResultLines. When testing app when inspecting the request the Results line added as boby parameters and to the request I am using Restsharp to comsume my api.. But when i put a breakpoint on my api i can only see that 23 lines has been recieve but values are always null by expanding the model which i marked as bold

Here my code to consume the Api

    public HeatResultResponse AddHeatWithList(List<ResultFileLine> rec,string HeatNum, string WebApiUrl)
    {
        RestClient client = new RestClient();
        client.BaseUrl = new Uri(WebApiUrl);
        var request = new RestRequest("Controller/InsertNewHeatList", Method.POST);
        request.AddHeader("Accept", "application/form-urlencoded");
        //client.AddHandler("application/json", new JsonDeserializer());
        request.AddHeader("Content-Type", "application/json");
        request.RequestFormat = DataFormat.Json;
        request.AddParameter("HeatNumber", HeatNum);
        rec.ForEach(r =>
           request.AddParameter("ResultLines", r)
         );
        IRestResponse<HeatResultResponse> response2 = client.Execute<HeatResultResponse>(request);
        return response2.Data;
    }

My Api Method looks like this

    public GetHeatIdResponse InsertNewHeatList(**HeatResultInsertListRequest model**)
    {
        GetHeatIdResponse resp = new GetHeatIdResponse
        {
            IsSuccess = false,
            Message = "",
            HeatId = 0
        };

        if (model == null)
        {
            resp.Message = "Request body is empty. Should contain JSON data";
        }
        else
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Heat heat = new Heat();
                    foreach(ResultFileLine model1 in         

Answers


Nambirajan S February 2016

Web Api by default will allow only 4mb request data.

IIS by default will accept 26 mb.

so iis will accept request but your app will accept will only 4mb data. Thats why request object is coming as null

You can increase this by modifying MaxRequestLength in HttpRuntime in Web.config

<httpRuntime maxRequestLength="50000"></httpRuntime>

Post Status

Asked in February 2016
Viewed 2,918 times
Voted 11
Answered 1 times

Search




Leave an answer