Call (Consume) Web API Post method in ASP.Net Core

Last Reply 6 months ago By dharmendr

Posted 6 months ago

Hello Sir,

i want to post data using web api in asp.net core.

i have taken only 1 textbox to post the data and save in database.

get method is working properly while going for post it is not working

i crated 2 different solution .

this is consume api code

[HttpPost]
public IActionResult Create(UserData student)
{
    HttpClient client = _api.Initial();

    var postTask = client.PostAsJsonAsync<UserData>($"api/user", student);
    postTask.Wait();
    var result = postTask.Result;
    if (result.IsSuccessStatusCode)
    {
        return RedirectToAction("Index");
    }
    return View();
}

above code for post

i got error 

StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Transfer-Encoding: chunked Server: Microsoft-IIS/10.0 X-SourceFiles: =?UTF-8?B?RTpcQmFja3VwIFByb2plY3RcQXBpRVlcQXBpRVlcYXBpXHVzZXI=?= X-Powered-By: ASP.NET Date: Fri, 22 Nov 2019 05:10:24 GMT Content-Type: text/html; charset=utf-8 }

--- this is api code----
[HttpPost]
public IActionResult PostStudent([FromBody]UserModel student)
{
    DataTable dt = new DataTable();
    if (!ModelState.IsValid)
        return BadRequest("Not a vaid model");
    using (ProgramDAL objDAL = new ProgramDAL())
    {
        var data = objDAL.SaveUser(student);
        // return data;
    }
    return Ok();
}

 

You are viewing reply posted by: dharmendr 6 months ago.