I am making a system with Entity Framework and Web Api. It has happened to me that I can only pass an object to the API, I cannot send it a string, why is this? Where is it configured that it only receives one object? and not only that, that the object has to be of a defined class, I don't know if it is understood, that's how it works for me.
[HttpPost]
[Route("Get")]
public L_Persona Get(L_Persona in_obj)
{
L_CabeceraUbicacion a = L_Persona.get(in_obj);
return a;
}
But this is not working for me:
[HttpPost]
[Route("Get")]
public L_Persona Get(int id)
{
L_Persona a = L_Persona.get(id);
return a;
}
If I have to pass a id
, for it to work I have to pass a person object with it id
and the other empty fields, when I could directly pass the id
. It also happens to me that I want to send an image and it also gives me an error, something like
415 unsupported media type
So how do I configure the Web Api to receive what I want to pass to it and not always an object?
Complementing the excellent answer of the colleague Flxtr , you should only specify in Route that it is going to receive an id:
Cheers!
The type of content you want to send must be specified in the request, the most frequent are JSON and XML:
Content-Type: application/json
Content-Type: text/xml
, or in your caseContent-Type: application/xml
To configure the parameters to receive in the Api, it must be
App_Start\WebApiConfig.cs
where it is assigned by default:When consuming the route, you will need to follow the structure:
Similarly, in that file, you can define custom routes.
to pass a string from the client you can use a query string in the URL, of course the url depends on your routing configuration, but the query string at the end does not change:
http://localhost/api/Controller/Action/?parameter1=parametervalue1¶meter2=parametervalue2
a real example:
on the client