Alonso Contreras
Alonso Contreras

Reputation: 675

How to send two or more parameter in web api service?

i have a web service methods using web api MVC, i have a method "Create", this receives two parameters (Objects) but when i try call this method from SOAP UI show this error:

{ "message": "Error.", "exceptionMessage": "No se pueden enlazar varios parámetros ('modelPerson' y 'modelCategory') al contenido de la solicitud.", "exceptionType": "System.InvalidOperationException", "stackTrace": " en System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n en System.Web.Http.Controllers.ActionFilterResult. }

My controller api:

    [HttpPost]
    [Route("Create")]
    public IHttpActionResult Create(Person modelPerson, Category modelCategory)
    {
        try
        {
            new PersonBLL().Create(modelPerson, modelCategory);
            return Ok("success");
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

In soap UI POST method:

{
  "name": "pepe",
  "age": 23,
  "address" : "xxxx 233"
  "pets":{                               //-> person model
     "id": 3002,
     "alias" : "vovi"
   }
 }
 ,
 {
    "id" : 101                         //category model (i need only id)
 }

But if i send only one parameter (modelPerson or categoryPerson), the call works perfectly, but with using two parameters not work :/

Upvotes: 2

Views: 1718

Answers (1)

Daniel May
Daniel May

Reputation: 8226

This isn't permitted by Web API.

Instead, you can use a single object that wraps the two:

public class CreateParameters
{
    public Person Person { get; set; }
    public Category Category { get; set; }
}

[HttpPost]
[Route("Create")]
public IHttpActionResult Create(CreateParameters parameters)
{
    try
    {
        new PersonBLL().Create(parameters.Person, parameters.Category);
        return Ok("success");
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }
}

With a request that looks something like this:

{
    "Person": {
        "name": "pepe",
        "age": 23,
        "address": "xxxx 233",
        "pets": {
            "id": 3002,
            "alias": "vovi"
        }
    },
    "Category": {
        "id": 101
    }
}

Upvotes: 1

Related Questions