0

I am trying to consume post method of web/api which takes string as parameter, but getting 405 Error : Api Controller Name: Getdata Action Method Name : postdata

please help to identify error.

public bool postdata(string fn)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:59968/api/Getdata");
                var task = client.PostAsJsonAsync("Getdata", fn);
                task.Wait();

                var res = task.Result;
                if(res.IsSuccessStatusCode)
                {
                    return true;
                }
            }
            return false;
        }

[HttpPost] public IHttpActionResult postdata(string p) { using (var context = new MediaEntities()) { mediatable m = new mediatable() { media_path = p, is_active = true }; context.mediatable.Add(m); context.SaveChanges(); return Ok(); } }

Muhammad Sami
  • 520
  • 2
  • 8
  • 21

1 Answers1

0

I think you have used action method names instead of controller name else provide details of your api.

client.BaseAddress = new Uri("http://localhost:59968/api/{controller_name}");

//HTTP POST
var postTask = client.PostAsJsonAsync("controller_name", fn);

This link might help you.

Snziv Gupta
  • 1,016
  • 1
  • 10
  • 21
  • have you tried above code replacing "controller_name" with you web api controller name. – Snziv Gupta Oct 13 '19 at 18:19
  • check your routing, try searching on SO you will find answer. check this https://stackoverflow.com/questions/15718741/405-method-not-allowed-web-api – Snziv Gupta Oct 13 '19 at 18:57