I've checked a few similar questions, but none of the answers seem to fit (or dumb it down enough for me). So, I have a really simple WebAPI to check if user with an email exists in DB.
AJAX:
var param = { "email": "ex.ample@email.com" };
$.ajax({
url: "/api/User/",
type: "GET",
data: JSON.stringify(param),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data == true) {
// notify user that email exists
}
else {
// not taken
}
}
});
WebAPI:
public bool Get(UserResponse id)
{
string email = id.email;
UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>();
ApplicationUserManager<ApplicationUser> manager = new ApplicationUserManager<ApplicationUser>(userStore);
ApplicationUser user = manager.FindByEmail(email);
if (user != null)
{
return true;
}
else
{
return false;
}
}
//helper class:
public class UserResponse
{
public string email { get; set; }
}
Now clearly, this doesn't work. The ajax call works fine, but how do I parse the json object to the WebAPI to be able to call it like id.email
?
EDIT
I can't pass the email address as a string, because the comma(s) mess up the routing.
The ajax call works fine, the object is sent to the WebAPI. The problem is I can't parse the object in code behind.