Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FromHeader Asp.NET Core binding to default value

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

Then, in Fiddler, I send the following request:

enter image description here

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

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?

like image 520
Guilherme Avatar asked Dec 06 '22 14:12

Guilherme


1 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 have to get values of x and y in string and convert it to int

like image 167
Usman Avatar answered Dec 19 '22 15:12

Usman