For passing an email address I'm using ajax with POST as type.
$.ajax({
url: "api/Search/UserByEmail",
type: "POST",
data: JSON.stringify({ emailAddress: userEmail }),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) { ... }
});
Controller:
[HttpPost]
public IEnumerable<Object> UserByEmail([FromBody] string emailAddress) { ... }
That's what Fiddler says:
POST http://localhost:52498/api/Search/UserByEmail HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:52498/#
Accept-Language: de-DE
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host: localhost:52498
Content-Length: 35
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
{"emailAddress":"[email protected]"}
Why is the emailAddress parameter always null?
[FromBody]attribute.[FromBody], my method is not even invoked. Here's the Routing:config.Routes.MapHttpRoute( name: "UserByEmailRoute", routeTemplate: "api/Search/UserByEmail", defaults: new { controller = "Search", action = "UserByEmail" } );api/Search/UserByEmail/emailAddresswithout that - just trying to rule out things that may be causing the problem :) In Fiddler, if you post[email protected]as the body does it work?