3

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:

  • passing the query as ?propertyId=teststring
  • Removing [FromQuery]
  • Removing Route
  • Changing headers
  • Combos of above

    public class RoomsController : ControllerBase
    {
        ...other stuff...
    
    [HttpPost, Route("{propertyId}")]
    public async Task<IActionResult> Post([FromQuery]string propertyId, [FromBody]List<CreateRoomRequestDto> value)
    {
        List<Guid> result = await _mediator.Send(new NewRoomRequest() { PropertyId = propertyId, NewRoom = value });
        return Ok(result);
    }
    }
    

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"]
    }
]

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.