0

I am using Asp.Net web forms with WebAPI 2. I have a method which works with GET but when I use POST then it doesn't work. If you see below there is a break point inside that method and it doesn't even reach there when calling POST. Strangely when I use Postman to test the method then it works with POST. What am I doing wrong?

My jquery code

$.ajax({
   url: 'MyController/GetUserDetail',
   type: 'POST',
   async: true,
   dataType: 'json',
   data:
   {
      "search": myvariable
   },
    success: function (response) {
   }
});

My Web API method. When using [HttpGet] and also changing jquery from POST to GET, then it works. But for POST this method is never called as breakpoint never hits and I don't receive any error either.

[HttpPost]
public User GetUserDetail(string search)
{      //breakpoint here
}

My route config

public static void Register(HttpConfiguration config)
{
   config.MapHttpAttributeRoutes();

   config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "{controller}/{action}/{id}",
      defaults: new { id = RouteParameter.Optional }
   );
}
Frank Martin
  • 3,147
  • 16
  • 52
  • 73
  • 1
    `I don't receive any error either` there will be an error in the response. You can use the network tab of the console to find it, after making the request in your JS code. – Rory McCrossan Feb 11 '20 at 11:56
  • what does the GET action look like? is it possible it's "swallowing" your request (maybe it does not have a [HttpGet] attribute)? – LongChalk Feb 11 '20 at 11:58
  • 1
    1) check the url is correct (browser network tab) - if you have deployed it might need to be `MyApp/MyController/MyAction` use `@Url.Action("GetUserDetail", "MyController")` to be sure (but not if it's in a .js file). 2) Remove the `[HttpPost]` attribute (for now at least, to rule it out) unless you have other overloads - it shouldn't be a POST anyway as it's clearly a GET "GetUserDetail". – freedomn-m Feb 11 '20 at 12:07
  • 1
    @LongChalk adding `[HttpGet]` would *limit* to GET, not indicate that's it's a get method. If there's only one method (as implied, but not specified in OP) then it doesn't need `[HttpGet]` – freedomn-m Feb 11 '20 at 12:09
  • @freedomn-m, perhaps it's not a GET/POST issue at all? perhaps you are calling it from a different area/controller in the postman and on the client side? have you considered debugging it by looking at the F12/Network? or requesting a 'MyController/GetUserDetail/' ? – LongChalk Feb 11 '20 at 14:14
  • Yeah I will check via F12/Network tomorrow when I go to work. – Frank Martin Feb 11 '20 at 17:09

2 Answers2

0

Looks like CORS issue. To handle CORS for Web API, add below settings in web.config file of Web API inside <system.webServer> section:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>

But there is a security risk involved in enabling CORS. A good explanation is here.

One more change is necessary in your JavaScript. There is a strange problem with 'data' setting of $.ajax(), so you need to embed parameter value within 'url' as query string:

url: 'MyController/GetUserDetail?search=' + myvariable,
as-if-i-code
  • 2,103
  • 1
  • 10
  • 19
  • I am using .Net 4.6.2 and seems like this is a known problem (as I saw a few posts about it with no solution) with it or it is related to some setting like you posted above about CORS. I am using Windows authentication. The same code works fine in earlier .Net versions. Will check this CORS setting tomorrow. – Frank Martin Feb 12 '20 at 13:12
0

with string search parameter you have not specified [FromBody] or [FromUri] so its bydefault take as fromUri for string type parameters. and you are passing parameter in the body. so, try to send parameter as query string...