Yesterday I test cross domain post against MVC successfully. So how about post to WebAPI?

But first try I failed and got error message like "Optional parameter 'user' is not supported by 'FormatterParameterBinding'.

after some research and test, I notice webapi [fromBody] parameters binding only take Model class as parameter.
//var data = "user=a" not owrk...string didn't have properties of user;
public string Test([FromBody] string user)
        {
            return "user:" + user;
        }

I have to change code like below:
public string Test([FromBody] User user)
        {
            return "user:" + user;
        }

  public class User
    {
        public string Name { get; set; }

        public string Pwd { get; set; }
    }
And test to post by jQuery
$.post('http://localhost:6660/api/task/test/0/t', { Name: 'a' ,Pwd:'b'});

and everything works


 
Comments are closed.