csharpvitamins / CSharpVitamins.ShortGuid

A convenience wrapper for dealing with base64 encoded Guids
MIT License
103 stars 20 forks source link

Transfer ShortGuid through ASP.NET Core API #10

Open Neon4eg opened 2 years ago

Neon4eg commented 2 years ago

Hello! When I'm trying to transfer ShortGuid inside some DTO through asp.net core api I get it as a JSON object on my web-client. It would be nice to have built-in json converter for ShortGuid to get string on client and parse to ShortGuid back in the API.

davetransom commented 2 years ago

Hi @Neon4eg - This has been mentioned before in #5 (it has some model binding details).

Just so it's clear, could you post an example DTO and the JSON it produces?

It's simple enough to add a TypeConverter since it doesn't add any more dependencies. I've been holding off on adding a JsonConverter because I don't want any external dependencies in this project. However, it looks like I can multi-target .NET Core 3.1 and be able to achieve this now, since System.Text.Json is part of the core.

Are you using .NET Core 3.1 and System.Text.Json?

Neon4eg commented 2 years ago

Hi davetransom Yes, I'm using .net 5 with System.Text.Json

I get this json in the browser

...
shortGuid: {
  "guid": "85d40989-9de2-4b93-93eb-0e66071ee278",
  "value": "iQnUheKdk0uT6w5mBx7ieA"
}

when I expect string "iQnUheKdk0uT6w5mBx7ieA"

DTO looks like this

public class SomeDto
    {
        public string FirstName { get; set; }     
        public ShortGuid ShortGuid { get; set; }
    }

I realized that actually I want to auto-convert all GUIDs to ShortGuid and back. I mean to have Guid in DTO and get ShortGuid in the browser. Same with values I get from query parameters etc