locked
404 error using web api services RRS feed

  • Question

  • User-251120743 posted

    how to consume a web api services and use in a mvc controller.

    this is my controller fetch values from my view:

    [HttpPost]
    public ActionResult add(Class2 cl)
    {using (HttpClient hc = new HttpClient())
    {hc.BaseAddress = new Uri(url);
    // var postTask = "";
    var postTask = hc.PostAsJsonAsync<Class2>("vaules",cl);
    postTask.Wait();
    var result = postTask.Result;//result value is 404 not found
    if (result.IsSuccessStatusCode)
    {return RedirectToAction("view");
    }}return View(cl);}

    api controller:

    [HttpPost]
    public void Post(Class2 cobj)
    {
    clo.name = cobj.name;
    clo.id = cobj.id;
    db.cl.Add(clo);
    db.SaveChanges();
    }

    error detail:

    + result {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Pragma: no-cache
    X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ2FuZXNocFxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEyXFByb2plY3RzXG12Y193ZWJhcGlcbXZjX3dlYmFwaVxhcGlcdmF1bGVz?=
    Cache-Control: no-cache
    Date: Tue, 02 May 2017 13:26:33 GMT
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Content-Length: 186
    Content-Type: application/json; charset=utf-8
    Expires: -1
    }} System.Net.Http.HttpResponseMessage

    how to rectify it?

    Tuesday, May 2, 2017 1:31 PM

All replies

  • User-2057865890 posted

    Hi learner_test,

    how to consume a web api services and use in a mvc controller.

    this is my controller fetch values from my view:

    [HttpPost]
    public ActionResult add(Class2 cl)
    {using (HttpClient hc = new HttpClient())
    {hc.BaseAddress = new Uri(url);
    // var postTask = "";
    var postTask = hc.PostAsJsonAsync<Class2>("vaules",cl);
    postTask.Wait();
    var result = postTask.Result;//result value is 404 not found
    if (result.IsSuccessStatusCode)
    {return RedirectToAction("view");
    }}return View(cl);}

    api controller:

    [HttpPost]
    public void Post(Class2 cobj)
    {
    clo.name = cobj.name;
    clo.id = cobj.id;
    db.cl.Add(clo);
    db.SaveChanges();
    }

    error detail:

    + result {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

    The problem could be the Uri the request is sent to. Could you show the request Uri?

    Try

    var postTask = hc.PostAsJsonAsync<Class2>("api/vaules",cl);
    
    
    [HttpPost]
    [Route("api/values")]
    public void Post(Class2 cobj)

    Best Regards,

    Chris

    Wednesday, May 3, 2017 3:19 AM