Web api not supporting POST method

后端 未结 6 1401
不知归路
不知归路 2020-12-10 10:47

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

       [HttpPost]
        public HttpResponseMessage Post(string schooltypeName)
        {
           


        
相关标签:
6条回答
  • 2020-12-10 11:02

    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

    0 讨论(0)
  • 2020-12-10 11:04

    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;
    
    0 讨论(0)
  • 2020-12-10 11:04

    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

    0 讨论(0)
  • 2020-12-10 11:06

    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

    0 讨论(0)
  • 2020-12-10 11:17

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

    0 讨论(0)
  • 2020-12-10 11:18

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

    0 讨论(0)
提交回复
热议问题