FromHeader Asp.NET Core绑定到默认值 [英] FromHeader Asp.NET Core binding to default value

查看:1116
本文介绍了FromHeader Asp.NET Core绑定到默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下基本控制器测试Asp.Net核心WebApi:

I'm testing Asp.Net core WebApi with the following basic controller:

[Route("test")]
public class TestController
{
    [Route("mult")]
    [HttpGet]
    public int Multiply(int x, int y)
    {
        return x * y;
    }
}

然后,在Fiddler中,我发送以下请求:

Then, in Fiddler, I send the following request:

由于某种原因,响应在输入方法时,Booth x y 被绑定为默认整数值。

And, for some reason, the response is 0. Booth x and y are binded to the default integer value when entering the method.

我也试过

[Route("test")]
public class TestController
{
    [Route("mult")]
    [HttpGet]
    public int Multiply([FromHeader]int x, [FromHeader]int y)
    {
        return x * y;
    }
}

但结果是一样的。我做错了什么?

But the result is the same. What I'm doing wrong?

推荐答案

表单标题接受 string not not int 所以你的代码应该是

form headers accept string not int so your code should be

[Route("test")]
public class TestController
{
        [Route("mult")]
        [HttpGet]
        public int Multiply([FromHeader]string x, [FromHeader]string y)
        {

            return Int32.Parse(x) * Int32.Parse(y);
        }
}

你必须得到 x 和 y 字符串中并将其转换为 int

you have to get values of x and y in string and convert it to int

这篇关于FromHeader Asp.NET Core绑定到默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆