ASP.NET Core Web API 中的加法操作

huangapple go评论72阅读模式
英文:

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(&quot;api/[controller]&quot;)]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpPost(&quot;sum&quot;)]
        public ActionResult&lt;int&gt; 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:

{ &quot;Sayi1&quot;: 5, &quot;Sayi2&quot;: 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:

&quot;type&quot;: &quot;https://tools.ietf.org/html/rfc7231#section-6.5.13&quot;,
&quot;title&quot;: &quot;Unsupported Media Type&quot;,
&quot;status&quot;: 415,
&quot;traceId&quot;: &quot;00-005af8933c5749315192cbff0b3953e0-86195ee93a314294-00&quot;

答案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: {&quot;n1&quot;: 5,&quot;n2&quot;: 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]方式:

ASP.NET Core Web API 中的加法操作

输出:

ASP.NET Core Web API 中的加法操作

[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,请将其替换为方法签名中的参数。其余语法将保持不变。

ASP.NET Core Web API 中的加法操作

输出:

ASP.NET Core Web API 中的加法操作

注意: 您可以参考此官方文档获取更多详细信息。

英文:

> 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]:

ASP.NET Core Web API 中的加法操作

Output:

ASP.NET Core Web API 中的加法操作

[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.

ASP.NET Core Web API 中的加法操作

Output:

ASP.NET Core Web API 中的加法操作

Note: You can refer to this official document for more details.

huangapple
  • 本文由 发表于 2023年7月14日 03:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定