英文:
Addition operation with ASP.NET Core Web API
问题
我正在尝试使用ASP.NET Core Web API的POST方法进行加法运算,但n1和n2始终为零。
using Microsoft.AspNetCore.Mvc;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPost("sum")]
public ActionResult<int> sum([FromForm] ToplamaModel model)
{
int sonuc = model.n1 + model.n2;
return sonuc;
}
}
public class ToplamaModel
{
public int n1 { get; set; }
public int n2 { get; set; }
}
}
API链接:https://localhost:7070/api/values/sum
原始JSON消息:
{ "Sayi1": 5, "Sayi2": 3 }
我正在使用Postman进行测试。
我询问了Chat GPT,它说要将FromForm
更改为FromBody
,但这并没有起作用,而是返回以下错误:
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "00-005af8933c5749315192cbff0b3953e0-86195ee93a314294-00"
如果你需要进一步的帮助,请告诉我。
英文:
I am trying to make addition operation with ASP.NET Core Web API post method but n1 and n2 are always zero
using Microsoft.AspNetCore.Mvc;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPost("sum")]
public ActionResult<int> sum([FromForm] ToplamaModel model)
{
int sonuc = model.n1 + model.n2;
return sonuc;
}
}
public class ToplamaModel
{
public int n1 { get; set; }
public int n2 { get; set; }
}
}
API link: https://localhost:7070/api/values/sum
Raw json message:
{ "Sayi1": 5, "Sayi2": 3 }
I am using Postman to test
I asked chat gpt and its say change FromForm to FromBody but that didn't work and instead returns:
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "00-005af8933c5749315192cbff0b3953e0-86195ee93a314294-00"
答案1
得分: 1
你应该使用 [FromBody],因为你的数据在请求体中,而不是表单中。
你收到了 "Unsupported Media Type" 错误,因为你可能在 Postman 的 "Headers" 选项卡中关闭了 "Content-Type" 标头。它应该被打开,并且值应为 "application/json"。
最后,你的 JSON 应该是:{"n1": 5, "n2": 3}
,这样它才能正确反序列化为你的 ToplamaModel 对象。
英文:
You should use [FromBody] as your data are in body, not form.
You got "Unsupported Media Type" because you probably turned off the header "Content-Type" in "Headers" tab in Postman. It should be turned on and have a value of "application/json".
And finally, your json should be: {"n1": 5,"n2": 3}
, so it can be deserialized properly to your ToplamaModel object.
答案2
得分: 0
我正在尝试使用ASP.NET Core Web API的POST方法进行加法操作,但n1和n2始终为零。
我已经使用您的示例进行了测试,使用[FromForm]
,我得到了精确的输出。不确定您是如何从Postman发送请求的。但是,可以通过在这种情况下同时使用[FromForm]
和[FromBody]
来实现这一目标,请求模式会稍微变化。
[FromForm]:
我尝试了以下的[FromForm]
方式:
输出:
[FromBody]:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPost("sum")]
public ActionResult<int> sum([FromBody] ToplamaModel model)
{
int sonuc = model.n1 + model.n2;
return sonuc;
}
}
注意: 如果要使用FromBody,请将其替换为方法签名中的参数。其余语法将保持不变。
输出:
注意: 您可以参考此官方文档获取更多详细信息。
英文:
> I am trying to make addition operation with ASP.NET Core Web API post
> method but n1 and n2 are always zero
I have tested using your sample with [FromForm]
and I am getting exact output. Its not sure how did you sent your request from postman. However, this can be achieved either using [FromForm]
and [FromBody]
in that scenario, request pattern would be changed a bit.
[FromForm] :
I have tried as following for [FromForm]
:
Output:
[FromBody] :
<!-- language: c# -->
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPost("sum")]
public ActionResult<int> sum([FromBody] ToplamaModel model)
{
int sonuc = model.n1 + model.n2;
return sonuc;
}
}
Note: If you want to use FromBody use replace it within your method signature. Rest of the syntax would remain same.
Output:
Note: You can refer to this official document for more details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论