Like many before me, I'm trying to pass multiple parameters to a Web API project. I've tried several solutions offered on this site with no luck. My latest failing iteration looks as follows:
The Controller
public class UserDTO
{
public int userID;
public string username;
}
[HttpPost]
public string Post([FromBody]UserDTO userDTO)
{
return userDTO.userID.ToString() + " " + userDTO.username;
}
The Web API route config
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
The jQuery
var apiUrl = "http://localhost:55051/api/User/Post";
var userDTO = {
userID: 5,
username: "testuser"
};
$.ajax({
type: "POST",
url: apiUrl,
data: JSON.stringify(userDTO),
datatype: "json",
contenttype: "application/json; charset=utf-8"
)};
Fiddler Output
Fiddler shows correctly passed JSON variables and in the Raw view I can see:
{"userID":5,"username":"testuser"}
On Execution
userID = 0
username = null
Help!
I figure that the issue is with Web API and it's difficulty in working with JSON parameters given that my POST appears to be correctly formatted in Fiddler. Any ideas?
datatype: "json",is supposed to bedataType: "json",