网络 API 2 接收 Json

Web API 2 Receiving Json

任何人都可以给我发送 json 数据从 jquery ajax 到网络 api 控制器的例子,客户端和服务器代码?例如,我想通过 ajax 作为 post 请求发送 {Name: "SomeName", Email: "SomeEmail"} 并在控制器处获取这些值...

服务器端检索值:

public class RequestModel()
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public MyWebApiController : ApiController
{
    public object Post(RequestModel model)
    {
        // Do something
        // Return same values back
        return model;
    }
}

客户端到 post 值:

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: '/Api/MyWebApi',
    data: { Name = "Bob", Email = "bob@example.com" }, 
    success: function (responseData) {

         // Do something on success, with the returned data
         alert("Email:" + responseData.Email + ", Name:" + responseData.Name);

    },
    error: function (jqXHR, textStatus, errorThrown) {

         // Display error?
    }
})