Luca | LeCarbonator
Explore posts from servers✅ Binding Nested values in RequestBody to Model
Consider the following model I use inside the MVC structure:
However, when responding to requests, this is instead formatted as
application/hal+json
, which would look like this:
As you can see, the property Url
has been moved into _links:external:href
.
Now I would like to receive a Collection in a [HttpPut]
request, but I'm having trouble figuring out how to properly parse the incoming application/hal+json
to my Model.
I've read online that Model Binding is probably the solution to this, but the vast majority of examples and explanations I could find were centered around Routes / Query parameters, not RequestBody formatting.
The part I did find mentioned ValueProviders, but I couldn't find out how to obtain the JSON data from the body and store it as keys there.
Any suggestions are appreciated, as I'm rather new to MVC architecture and C# in general.5 replies
Specifying 4xx Details
I have a
POST
request that may cause a duplicate entry if it were to execute. As a response, I send back 409 Conflict
to make clear what happened.
However, I have some trouble adding some detail to explain what exactly happened.
First, I tried the following, which is not RFC 9110 compliant because it returns a simple text/plain
:
return Conflict("A string explaining which property is already reserved");
When only returning Conflict();
, the response body is application/problem+json
as expected:
How can I add the "detail"
property when returning Conflict()
without altering the return type?8 replies
✅ Is it possible to access a Controller's URI at runtime?
I'm not well versed in MVC structures, so this very well could be a XY problem. Let me know if it is a bad idea to begin with.
I'm trying to create a GET endpoint for
/collections
which returns Collections. One Collection
always contains a nested Model User
.
On its own, this is not a problem. However, I am creating my API based around application/hal+json
, which requires Resources to have a link to point to their exact location.
So when accessing a returned Collection
, I could use its related link if I wish to access it directly instead.
It looks sort of like this:
Endpoints:
/collections
/collections/{uuid}
This is all well and good, but I have some concerns with generating the embedded collection's link.
If I hardcode it as $"http://foo-api/collections/{uuid}"
, then I would have to change the domain in every location where I did it.
Is it possible to access the CollectionController#GetById(string uuid)
somehow and extract its URI at runtime?20 replies