C
C#2y ago
Arkobat

Deserialize wrapper object in route

Hello. Does anyone know if it is possible to map deserilize a custom wrapper object in a route? IDs in my system is strings, and we have therefore made an External class. The only point of this, is to improve code readability, to easy be able to view what is ids, and what is not. We have made a serilizer, that maps maps the class to a string in any JSON schemes. However, this serilizer does not seem to work, when we use the class in a route. Can we do it via a serilizer or some middleware, or do I have to take a string as input, and map it to ExternalId in my controller?
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething([FromRoute] ExternalId id)
{
var result = await _service.GetSomething(id);
return result.ToDTO();
}
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething([FromRoute] ExternalId id)
{
var result = await _service.GetSomething(id);
return result.ToDTO();
}
public class ExternalId
{
public string Id { get; set; }

public ExternalId() {}
public ExternalId(string id)
{
Id = id;
}
public class ExternalId
{
public string Id { get; set; }

public ExternalId() {}
public ExternalId(string id)
{
Id = id;
}
6 Replies
Angius
Angius2y ago
This should work, just maybe get rid of the constructor Serialization generally requires a default ctor to be present, the easiest way to do that is to... just not define any ctor at all
Arkobat
Arkobat2y ago
I do have a no args construtor, just removed it to keep the code simpel. However, the id is just a ExternalId, with the inner string being null
Arkobat
Arkobat2y ago
Angius
Angius2y ago
Try [HttpGet("{Id}")] Casing might matter
Arkobat
Arkobat2y ago
Ahh... I found the fix, thanks to you! The issue was not spacing, but that my parameter was also named id
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething(ExternalId id)
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething(ExternalId id)
To fix this, I have to rename my outer id to something else
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething(ExternalId externalId)
[HttpGet("{id}")]
public async Task<MyDTO> GetSomething(ExternalId externalId)
Thanks for the help!
Angius
Angius2y ago
Ah, yeah, that makes sense Glad you got it sorted
Want results from more Discord servers?
Add your server
More Posts
VS JB Mono font not looking like Rider JB Mono font [Answered]I have to use vs 2022 due to rider not supporting xaml hot reload and am trying to make it look simiJsonSerializer.DeserializeAsync() randomly hangs indefinitely [Answered]```cs private static async Task<T> TryReadDataFromFile<T>(string basePath) where T : class { vardotnet6 project failing on static abstracts with preview features set to true``` C:\Users\Kellen James\source\Repos\ClemBot\ClemBot.Api\ClemBot.Api.Core\Features\Tags\Edit.cs(51I need to serialize a bunch of non-primitive types. How would I do this?I'm using JsonSerializer.serializeToUtf8Bytes I need to serialize a class that contains a bitmap, anWhen to use ValueTask?Despite having used C# for a while I'm... not *that* familiar with async. I know people sometimes saHow would you design a solution to this problem?unsure what would be the best approach - I have a SQL table that stores SMS 3rd party providers. EWrite a String manipulation program [Answered]I have two strings. One of them contains completely the entire other string. While that other stringC++ Interop - writing a shim to use an existing c++ library [Answered]So I'm currently trying to make a shim for a C++ library that I can then call from C#. The original Are there dedicated .net framework or ASP discords? [Answered]Are there dedicated .net framework or ASP discords? If so pls link 😳How to get these values from the dynamic json object ?I wanna assign the values for id and password to strings so I'll have something to work with after t