Web api not supporting POST method

In my web api controller i have a function with following codes

       [HttpPost]
        public HttpResponseMessage Post(string schooltypeName)
        {
            _schoolTypeService.RegisterSchoolType(schooltypeName);

            var message = Request.CreateResponse(HttpStatusCode.Created);

            return message;
        }

When i am calling with fiddler i am getting this error
{"Message":"The requested resource does not support http method 'POST'."}

my fiddling parameters are

Header

User-Agent: Fiddler

Host: myhost:8823

Content-Type: application/json; charset=utf-8

Content-Length: 26

Request body
{"schooltypeName":"Aided"}

Requesting url are
http://myhost:8823/SchoolType

( i configured url ,GET is working with this url)

Whats wrong here ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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; } }

Method 2

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;

Method 3

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

Method 4

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

Method 5

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

Method 6

What helped to me at the end was adding the Route attribute, and just repeating there the same route which as registered globally.

Method 7

If you add attribute [RoutePrefix("samplePrefix")] for controller class, but not add attribute [Route("sampleRoute")] for specified post method, this issue may occurs.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x