FromHeader Asp.NET Core binding to default value

I am testing an Asp.Net WebApi core with the following basic controller:

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

      

Then in Fiddler I send the following request:

enter image description here

And for some reason the answer is 0. When you enter the method, the values x

and y

are associated with the default integer value.

I also tried:

[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 am I doing wrong?

+2


source to share


2 answers


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);
        }
}

      



you need to get the values x

and y

in string

and convert them toint

+9


source


Apparently, it was initially assumed that this would not be needed, and they are waiting for feedback. However, it looks like it will probably be at 2.1.0 according to issue 5859 .



+3


source







All Articles