[英]Using web API with parameters in JSON with FromUri or FromBody
I am trying to create an example with web api
. 我正在尝试使用Web api
创建一个示例。 I want the received parameters in JSON with fromuri
or frombody
. 我想要使用fromuri
或frombody
在JSON中接收到的参数。 When I create the client I want to send an object or JSON with the information. 创建客户端时,我想发送包含信息的对象或JSON。
In my web API I have this 在我的Web API中,我有这个
[HttpPost]
public IHttpActionResult Post([FromUri]Peticion peticion)
{
return Ok(peticion);
}
and in my client 和我的客户
Peticion obj = new Peticion();
obj.cliente = 2;
obj.factura = 22;
string DATA = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
var client = new HttpClient();
HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
HttpResponseMessage message =
client.PostAsync("http://localhost:57418//api/Deudor/",content).Result;
if (message.IsSuccessStatusCode)
{
string result = message.Content.ReadAsStringAsync().Result;
}
But I need in my client received the JSON. 但是我需要在我的客户端中收到JSON。 In the web API I need to receive two parameters (cliente, factura) and in my client I need to received the information of that client (name, invoice etc) in JSON. 在Web API中,我需要接收两个参数(cliente,factura),在我的客户端中,我需要以JSON接收该客户端的信息(名称,发票等)。
I used postman, and received the JSON with the information. 我使用邮递员,并收到了包含信息的JSON。 But in my client C# I couldn't. 但是在我的客户C#中,我做不到。
Regardless of using .net core or framework, you can use HttpClient to send Get and Post requests to your API. 无论使用.net核心还是框架,都可以使用HttpClient将Get和Post请求发送到您的API。
POST request: POST请求:
HttpClient client = new HttpClient();
var content = new StringContent(JsonConvert.SerializeObject(YourObject), Encoding.UTF8, "application/json");
client.PostAsync("your url", content);
Then you can get content [FromBody] You can use Post requests and get parameters from url, but it's better to use get in that case. 然后,您可以获取内容[FromBody]您可以使用Post请求并从url获取参数,但在这种情况下,最好使用get。
Get request: 获取请求:
HttpClient client = new HttpClient();
client.GetAsync("your url/api/controller/id/{id}");
In both cases you need to set your routes for your methods with request types (GET, POST). 在这两种情况下,都需要使用请求类型(GET,POST)为方法设置路由。 eg: in the get example above: 例如:在上面的get示例中:
[Route("api/[controller]")
public class SomeController
{
[HttpGet]
[Route("id/{id}")]
public someGetMethod(int id){
//do something with id here...
}
}
In Web API, if the controller method's parameter is not a "simple" type, it tries to get the value from the body. 在Web API中,如果控制器方法的参数不是“简单”类型,则它将尝试从主体获取值。 So I think you want to remove [FromUri]
in your Post()
parameters because you're actually sending that information in the request body. 因此,我认为您想删除Post()
参数中的[FromUri]
,因为您实际上是在请求正文中发送该信息。
Source: https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api 来源: https : //docs.microsoft.com/zh-cn/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.