简体   繁体   中英

MVC4 Web API Post Parameter Null

Web API Post parameters are always Null

ASP.NET Web API POST parameter is null

What worked for these folks, doesn't seem to be working for me. I have almost exactly the same setup but still getting null in my second controller. The first one works just fine, but I can't replicate it in a second controller.

API Call

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

Controller class

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;
    }
}

Like I said, this works inside of another controller (UsersController). When I pass { "First_Name": "John", "Last_Name": "Doe" } I properly receive a User object in the Post method public void Post([FromBody]User user)

What am I missing here?

Try with the modified code below:

API Call:

$.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);
});

Controller class:

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

Creds class:

Unchanged.

Follow the below Code

Web API Controller class

   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;
    }
}

Either Use API Ajax call in the following manner

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);
            }
            });

Create a client application that will call webapi in the following manner

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();

@StephenMuecke had the correct answer for me. It was because my class had a parameterless constructor.

I needed to do this to my Creds class

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;
    }
}

Big thank you to @StephenMuecke

You don't have to create a model to take multi post parameters. You can use dynamic object instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM