0

I am a new with asp.net web api, in asp.net web api, how i can let it to auto bind more then one parameter for the action, just like below.

public class DemoController : ApiController
{
    public class DemoModel
    {
        public string X { get; set; }

        public string Y { get; set; }

        public int Z {get; set;}
    }

    [HttpPost]
    public DemoModel GetParameter(string x,string y,int z)
    {
        return new DemoModel(){X = x, Y=y, Z=z};
    }
}

In a html page,i use jquery ajax to post data to the api,and i got a 404 erorr.
I just change the default Route .
Default:

api/{controller}/{id}  

Changed:

api/{controller}/{action}/{id}
  • Not sure what is the problem you have. These three will be bound to some values successfully as long as you have them as your request params – Andrei Oct 15 '15 at 10:14
  • I use the HttpPost attribute to tag the action , and i request the api with post,but the parameters can't bind successfully. – programmer.zheng Oct 15 '15 at 11:40
  • The problem must be with the way you do post,cause there is nothing wrong with the action. Can you show us the posting code? – Andrei Oct 15 '15 at 12:34
  • $.ajax ("/api/demo/GetParameter", {data:{"x":"xxx","y":"yyy","z":234}, success:function(data){}} – programmer.zheng Oct 15 '15 at 13:17

2 Answers2

0

First of all, you should wrap your parameters into some kind of a DTO (Data Transformation Object) model, and then do the conversion into DemoModel Instead of calling

return new DemoModel() {X = x, Y = y, Z = z}

you could change the signature of your action to something like this:

[HttpPost]
public IHttpActionResult GetParameter(DemoModelDto model)
{
    if (ModelState.Valid)
    {
        var demoModel = DemoModel.FromDto(model);
        var url = Url.Route("{DefaultApi}", new {controller = "MyController", action = "MyAction", id = yourIdIfYouAreGeneratingIt});

        return Created(url, model);
    }

    return BadRequest(ModelState);
}

What's going to happen here is that ModelBinder will take the request and extract those x, y and z, and create a DemoModelDTO for you which you can use to create your DemoModel. The return result will depend on whether the ModelState was valid. If yes, Created will be returned which is HTTP status code 200 for OK (or 201 for Created), or if you have ModelState was not valid it will return BadRequest which is 400.

In addition to this, if you want to send X, Y and Z as URL parameters you will need to issue a request like:

POST http://localhost/Demo?x=1&y=2&z=3

Either using your current implementation or the one above will result in the values being passed to Web API.

Huske
  • 9,186
  • 2
  • 36
  • 53
  • Is there any way else?Because i don't want to create DTO model for every action of post. – programmer.zheng Oct 15 '15 at 13:09
  • You can ignore the DTO and use the parameters. ModelBinder will bind to them by their name as it discovers them. – Huske Oct 15 '15 at 13:10
  • But i dont't want transmit the parameters with get method – programmer.zheng Oct 15 '15 at 13:14
  • You don't have to. Decorate your action with HttpPost and you'll get them back after posting. – Huske Oct 15 '15 at 21:24
  • `public IHttpActionResult GetParameter( string x,string y,string z)` How can i let it works with ajax post method,and i've decorate the `HttpPost` attribute for the action. – programmer.zheng Oct 23 '15 at 16:30
  • have you tried using Postman or Fiddler to POST a request to this controller and debug the process? – Huske Oct 25 '15 at 11:00
  • No,but i wrote a html page and post request with ajax. – programmer.zheng Oct 28 '15 at 18:05
  • Try posting either as query string parameters like POST http://localhost/Demo?x=1&y=2&z=3 or add [FromBody] attribute in front of all three parameters in GetParamter method, like GetParameter([FromBody] string x, [FromBody] string y, [FromBody] string z). See what works for you. – Huske Oct 29 '15 at 23:24
0

If you want to be able to pass dynamic parameters without explicitly specifying them on the Action method, then Make the Action parameterless then call with the parameters as query string values on the client.
Then you can retrieve the QS values with something like this:

 var queryVals = Request.RequestUri.ParseQueryString();    
 var x = queryVals["x"];
 var y = queryVals["y"];
 var z = queryVals["z"];
Kofi Amparbeng
  • 158
  • 1
  • 10
  • Since you do not want to use GET, please take a look at this question
    http://stackoverflow.com/questions/17832784/how-to-get-post-data-in-webapi
    – Kofi Amparbeng Oct 15 '15 at 14:25