none
HttpPost WebAPI method when hosted on server Method not allowed error RRS feed

  • Question

  • I have hosted webAPI method on IIS and trying to call it but throwing different errors like.

    • Internal server error 500
    • 405/Method not allowed
    • callback parameter should be passed in the URL.

    Please find the code below

    --------------------------------------

    public class TestController : ApiController
    {

    [Route("api/Test/Email/{trioInfo}")]
            [HttpPost]
            public HttpResponseMessage Email(Product product)
            {
                
                string response = string.Empty;
                try
                {

                   //Do logic

                    

                }
                catch (Exception ex)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, response + "  \n" + ex.Message);
                }
                var jsonResponse = new { result = response };
                return Request.CreateResponse(HttpStatusCode.OK, jsonResponse);
            }

    }

    Have coded below code at WenAPIConfig.cs

    ------------------------------------------------------

     public static void Register(HttpConfiguration config)
            {
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                   routeTemplate: "api/{controller}/{action}/{id}",
                   //routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

                var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
                config.Formatters.Insert(0,jsonpFormatter);

            }

    web.config

    ------------------------

    <system.webServer>
    <httpProtocol>
    <customHeaders>
    <clear />
    <add name="Access-Control-Allow-Origin" value="*" />
    <!--<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />-->
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
    </httpProtocol>

    </system.webServer>

    Used script

    -------------------------------

    function Email() {

            var product =
                {
                   Name:'Product1',

                }
            product = JSON.stringify(product);
            $.ajax({
                type: "GET",
                url: urldata + "/api/Test/Email/",
                data: product,
                contentType: "application/json; charset=utf-8",
                //dataType: 'Jsonp',
                success: function (response) {
                    debugger
                    alert("Hello: " + response.result);
                },
                failure: function (response) {
                    debugger;
                    alert(response.responseText);
                },
                error: function (response) {
                    debugger;
                    alert(response.responseText);
                }
            });


    Thursday, April 12, 2018 6:24 PM

All replies

  • Your Ajax is sending a GET, but the method in the controller is expecting a POST. 

    WebAPI issues can be discussed at the ASP.NET forums WebAPI forum.

    https://forums.asp.net/

    Friday, April 13, 2018 2:01 AM
  • Hello Rajeswari,

    You need to repost a new thread to Asp.Net forum for better support because the question is more related to Asp.Net.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 13, 2018 4:49 AM