In my web api controller i have a function with following codes
[HttpPost]
public HttpResponseMessage Post(string schooltypeName)
{
For future readers. I found this question..but found (my) answer elsewhere.
I decorated the method with the attribute seen below.
[System.Web.Http.HttpPost]
public MyOutputObject DoSomething([FromBody]MyInputObject args)
{
Console.Writeline(args.ToString());
return new MyOutputObject();
}
My client code (C#, console app) for completeness. Please note it is NOT an good example of async/await since it has .Result in it.
private static Task<MyOutputObject> MyClientCall(string baseAddress, MyInputObject args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
/* Your class name would be "MyEntityController" most likely */
string serviceUrl = baseAddress + @"api/MyEntity/DoSomething";
HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, args).Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("ERROR: :( " + response.ReasonPhrase);
return null;
}
Task<MyOutputObject> wrap = response.Content.ReadAsAsync<MyOutputObject>();
return wrap;
}
I found my answer here:
http://blog.dontpaniclabs.com/post/2013/01/23/That-Pesky-Requested-Resource-Does-Not-Support-HTTP-Method-POST-Error-When-Using-MVC-Web-API
See the using namespace at the top of the controller, if you're using System.Web.Mvc, then this problem might be occurred:
Use this:
using System.Web.Http;
Please Check your GET Action method name you are using. Chances are you might be using the same Route names to GET method and POST method and expecting the result.
Example :
Controller name : StudentController
[Route("api/Student/names")]
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "student1", "student2" };
}
Url for method (GET): http://localhost:59342/api/student/names
[HttpPost]
[Route("api/Student/names")]
public String Post(String name)
{
return "success";
}
For POST method to url : http://localhost:59342/api/student/names
Then you will get the above mentioned error
Solution: Change the POST action method name like below
[HttpPost]
[Route("api/Student/getName")]
public String Post(String name)
{
return "success";
}
Then url which is used to get the response for post method is :
http://localhost:59342/api/student/getName
The Problem comes down to this:
if your routes in startup is registered with routes.MapRoute(
you must decorate your post methods with [System.Web.Mvc.HttpPost]
If your routes in startup is registered with Routes.MapHttpRoute(
you must decorate your post methods with [System.Web.Http.HttpPost]
if you use MapRoute()
with [System.Web.Http.HttpPost]
it wont work
if you use MapHttpRoute()
with [System.Web.Mvc.HttpPost]
it wont work
What helped to me at the end was adding the Route
attribute, and just repeating there the same route which as registered globally.
Change your action to be like Post([FromBody]string schooltypeName)
as by default string type is expected to come Uri.
Updated:
Change your body to just "Aided"
as currently you would need a class to make the deserialiation work otherwise (ex:class School { public string SchoolTypeName { get; set; } }