Luca | LeCarbonator
Luca | LeCarbonator
Explore posts from servers
CC#
Created by Luca | LeCarbonator on 11/18/2024 in #help
✅ Binding Nested values in RequestBody to Model
Consider the following model I use inside the MVC structure:
public class Collection
{
/// <summary>
/// The UUID of this Collection
/// </summary>
public required string Id { get; set; }
/// <summary>
/// The external Url representing the entries of this collection
/// </summary>
public required string Url { get; set; }
/// <summary>
/// The date the Collection was created at.
/// </summary>
public required DateOnly CreatedAt { get; set; }
}
public class Collection
{
/// <summary>
/// The UUID of this Collection
/// </summary>
public required string Id { get; set; }
/// <summary>
/// The external Url representing the entries of this collection
/// </summary>
public required string Url { get; set; }
/// <summary>
/// The date the Collection was created at.
/// </summary>
public required DateOnly CreatedAt { get; set; }
}
However, when responding to requests, this is instead formatted as application/hal+json, which would look like this:
{
"_links": {
"self": {
"href": "string"
},
"external": {
"href": "string"
}
},
"createdAt": "2024-11-18"
}
{
"_links": {
"self": {
"href": "string"
},
"external": {
"href": "string"
}
},
"createdAt": "2024-11-18"
}
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
CC#
Created by Luca | LeCarbonator on 11/15/2024 in #help
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:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.10",
"title": "Conflict",
"status": 409,
"traceId": "uuid string"
}
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.10",
"title": "Conflict",
"status": 409,
"traceId": "uuid string"
}
How can I add the "detail" property when returning Conflict() without altering the return type?
8 replies
CC#
Created by Luca | LeCarbonator on 11/13/2024 in #help
✅ 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}
{
"_links": {
"self": {
"href": "http://foo-api/collections"
}
},
"_embedded": {
"collection": [

{
"fooProp": "I'm a specific collection",
"_links": {
"self": {
"href": "http://foo-api/collections/62fcac86a39a11ef"
}
}
}

]
}
}
{
"_links": {
"self": {
"href": "http://foo-api/collections"
}
},
"_embedded": {
"collection": [

{
"fooProp": "I'm a specific collection",
"_links": {
"self": {
"href": "http://foo-api/collections/62fcac86a39a11ef"
}
}
}

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