tony09uk
tony09uk

Reputation: 2991

Web Api string parameter (fromQuery) always null when posted with value (FromBody)

I am using web api 2 and .net core 2.0

I'm trying to post to an endpoint but the propertyId in the in the query is always null, but the value in the body is populated. If I change propertyId to an int, it gets populated.

After LOTS of reading I found this, which has "confirmed" - for me - that what I've done should be working. It's not so I don't know what I'm missing. There are many threads on this issue but none have helped me. Could someone please advise what i'm missing?

I have tried a number of variations, including:

http://localhost:49942/api/Rooms/testString

the postman script

POST /api/Rooms/testString HTTP/1.1
Host: localhost:49942
Cache-Control: no-cache
Content-Type: application/json
Postman-Token: f15b569d-84a2-4bde-bdf0-b1cdf3fff975

[
    {
        "Tag":"30dd879c-ee2f-11db-8314-0800200c9a66",
        "Name":"testName",
        "Description": "tesDescription",
        "Length": 1.5,
        "Width": 1.8,
        "Dimension": 5,
        "DimensionText": "some DimensionText test",
        "PhotoUrls": ["klklkl", "oioioioii"]
    },
    {
        "Tag":"30dd879c-ee2f-11db-8314-0800200c9a66",
        "Name":"testName2",
        "Description": "tesDescription2",
        "Length": 1.2,
        "Width": 1.9,
        "Dimension": 5,
        "DimensionText": "some DimensionText test2",
        "PhotoUrls": ["klklklwewe", "oioioioiinmnmnm"]
    }
]

Upvotes: 3

Views: 3756

Answers (1)

Turner Bass
Turner Bass

Reputation: 821

What you need to do is name the [FromQuery] attribute like this

[FromQuery(Name = "propertyId")]

I've been looking through the .NET Core routing code recently and it seems like [FromQuery] just needs some help, from time to time, to know which pieces of the query string to map from.

EDIT: Oh, and also take your route attribute off so that it's clear where the parameter is coming from. As well as make your request use an actual query string like

?propertyId=testString instead of making it the ID on the route.

Upvotes: 1

Related Questions