MVC4 Web API Post参数Null

时间:2015-03-30 23:25:44

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

Web API Post parameters are always Null

ASP.NET Web API POST parameter is null

对这些人有用的东西似乎对我不起作用。我有几乎完全相同的设置,但仍然在我的第二个控制器中为null。第一个工作正常,但我无法在第二个控制器中复制它。

API调用

$.ajax
({
    type: "POST",
    url: "http://localhost:52441/api/login",
    data: { "Username": "test", "Password": "ptest" }
})
.done(function (data) {
    //var json = $.parseJSON(data);
    console.log(data);
});

控制器类

public class LoginController : ApiController
{
    public bool Post([FromBody]Creds creds)
    {
        return false;
    }
}

Creds class

public class Creds
{
    public string Username { get; set; }
    public string Password { get; set; }

    public Creds(string user, string pass)
    {
        this.Username = user;
        this.Password = pass;
    }
}

就像我说的,这适用于另一个控制器(UsersController)。当我通过{ "First_Name": "John", "Last_Name": "Doe" }时,我在Post方法public void Post([FromBody]User user)

中正确接收了User对象

我在这里缺少什么?

4 个答案:

答案 0 :(得分:0)

尝试使用以下修改后的代码:

API调用:

$.ajax
({
    type: "POST",
    url: "http://localhost:52441/api/login/validate",
    data: { "Username": "test", "Password": "ptest" }
})
.done(function (data) {
    //var json = $.parseJSON(data);
    console.log(data);
});

控制器类:

public class LoginController : ApiController
{
    [HttpPost]
    public bool Validate(Creds creds)
    {
        return false;
    }
}

Creds class:

不变。

答案 1 :(得分:0)

按照以下代码

Web API 控制器类

   public class LoginController : ApiController
        {
            public bool Validate([FromUri]Creds creds)
            {
                return false;
            }
        }

Creds class

public class Creds
{
    public string Username { get; set; }
    public string Password { get; set; }

    public Creds(string user, string pass)
    {
        this.Username = user;
        this.Password = pass;
    }
}

以下列方式使用API​​ Ajax调用

var RequestZipCode = { Username: "test", Password: "ptest" };
            $.ajax({
                type: "GET",
                data: Creds,
                url: "api/Name of the Controller/",
                contentType: "application/json",
                success: function (data){
                    ShowData(data);
            }
            });

创建一个将以下列方式调用webapi的客户端应用程序

StringBuilder sbAPIRequest = new StringBuilder();
 sbAPIRequest.Append("?Username=test");
                sbAPIRequest.Append("&Password=ptest");

                HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sbAPIRequest.ToString());

                httpWReq.Method = "GET";
                httpWReq.ContentType = "application/json;charset=UTF-8";
                httpWReq.ContentLength = 0;

                HttpWebResponse Httpresponse = (HttpWebResponse)httpWReq.GetResponse();
                string responseString = new StreamReader(Httpresponse.GetResponseStream()).ReadToEnd();

答案 2 :(得分:0)

@StephenMuecke对我有正确的答案。这是因为我的班级有一个无参数构造函数。

我需要对我的Creds类

这样做
public class Creds
{
    public string Username { get; set; }
    public string Password { get; set; }

    public Creds()
    {
        this.Username = string.Empty;
        this.Password = string.Empty;
    }

    public Creds(string user, string pass)
    {
        this.Username = user;
        this.Password = pass;
    }
}

非常感谢@StephenMuecke

答案 3 :(得分:0)

您不必创建模型来获取多个帖子参数。您可以改用动态对象。

[HttpPost]
public SomeObject GetSomeObject([FromBody] dynamic postData) {
    var a = postData.[property name];
}