Parameterize with Restshap

Asked

Viewed 27 times

0

Hello, I am trying to pass a POST to my Webapi using Rastsharp, but I’m having difficulty passing the parameters...

RestClient client = new RestClient("http://localhost:18256");
RestRequest request = new RestRequest("api/BancoWS", Method.POST);

//add os parametros
request.AddObject(empresa);
request.AddParameter("host", host);

request.OnBeforeDeserialization = resp => { 
    resp.ContentType = "application/json; charset=utf-8;"; };

result = client.Execute<SalvarDto>(request);

{"Message":"The requested Resource does not support http method 'POST'."}

Statuscode: Methodnotallowed

Here then I have the webApi details

// POST: api/BancoWS
public SalvarDto Post(Empresa model, string host)
{
    SalvarDto banco = _bancoRepository.GerarBanco(model, host);
    SalvarDto dto = _bancoRepository.GerarBD((Banco)banco.Model);

    return dto;
}

If I remove the parameter 'host'then yes I succeed, but in this case it is necessary, then I have this problem there that does not arrive the value in the method Post.

  • It depends on how your route is set, probably the host parameter is being sent in the URL and therefore the problem of MethodNowAllowed. How do you want to receive this parameter? In the same URL or with formdata?

1 answer

1


The way you’re going host it will be integrated into the URL, this way: api/BancoWS?host=valor. Therefore, to receive the value correctly you may need to explain with [FromUri]

public SalvarDto Post([FromBody]Empresa model, [FromUri]string host)
{
    SalvarDto banco = _bancoRepository.GerarBanco(model, host);
    SalvarDto dto = _bancoRepository.GerarBD((Banco)banco.Model);

    return dto;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.