FromHeader Asp.NET Core binding to default value

Guilherme

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?

Usman

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP .NET Core model binding

ASP.NET Core: Default value for Guid parameter in web api?

.NET Core <select> With Default Value?

ASP.NET Core Binding to a dictionary

Data binding in ASP.NET Core

ASP.NET Core model binding and integers

Model Binding with ASP.NET Core

ASP.NET Core trouble in model binding

ASP.NET Core - Model binding testing

asp.net core not binding current User

Authorization token not binding in ASP.NET Core

Model binding Asp net core 2.2

ASP.NET Core Web API: how to assign value of null to a missing property in the request when parameter binding

Model binding / ViewModel binding in ASP.Net Core MVC

Data binding along with the value in ASP.NET

ASP.NET MVC binding decimal value

How to Change ASP.NET Core's Built-In Web-Server (Kestrel) Default IP Binding, from 127.0.0.1 to 0.0.0.0?

ASP.NET Core MVC default routing

ASP .NET Core default language is always English

Asp.net core default route

ASP.NET Core - Overriding the default ControllerFactory

ASP.NET Core default swagger page

ASP.NET Core modelbinding pickups up default enum value in case when the enum item is not available

Can not set decimal value as default in dropdownlistfor asp.net core using javascript

ASP.NET Core - Cannot set default value '1' of type 'System.Int32' on property 'MerchantStatus'

Issue Creating a Unique Default Value for Id using Grid in Telerik ASP.NET Core for Ajax

ASP.NET WebApi Changing default parameter binding error message

Set default value in Tag helper .net core

ASP.Net Core default route as /Home with it shown by default