C
C#17mo ago
Amira

❔ How to convert query string to an Enum in a custom way

I have the following code: .
[HttpGet]
public async Task<IActionResult> GetCarsAsync([FromQuery] GetCarsApiQuery query)
{

}

public class GetCarsApiQuery
{
public string? VehicleId { get; set; }


public VehicleType? VehicleType { get; set; } //Cannot change its type to string
}

public enum VehicleType {
Car,
Bike,
Undefined
}
[HttpGet]
public async Task<IActionResult> GetCarsAsync([FromQuery] GetCarsApiQuery query)
{

}

public class GetCarsApiQuery
{
public string? VehicleId { get; set; }


public VehicleType? VehicleType { get; set; } //Cannot change its type to string
}

public enum VehicleType {
Car,
Bike,
Undefined
}
By default, If I send a GET request with a VehicleType = Foo, the framework will return a bad request. I want to change this behavior so that when it tries to convert the query string to VehicleType, it either succeed or returns VehicleType.Undefined In other words, I am looking for something like a custom JsonConverter but that works on query strings rather than json body any ideas ?
20 Replies
Omnissiah
Omnissiah17mo ago
what happened did you close and reopen this
Amira
AmiraOP17mo ago
yup, the other one was not so clear, since it was very long IT doesn't help some one who wants to read as quickly as possible
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
I can't change the type to a string for sure
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
because I work in a company where there is some administrative overhead if you want to touch the API code :/ why you are changing string here ?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
I know :/
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
yeah but here you changed the type of VehicleType to string I mean here public string? VehicleType { get; set; } //Cannot change its type to string it was public VehicleType? VehicleType {get; set;}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
What's the messy alternative ? Curious to know about it
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
what's this 😄
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
okay thanks, will check it !
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Amira
AmiraOP17mo ago
I didn't know about Bing using an AI now 😅
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server