C
C#•2y ago
nael4slayer

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 this stage. Some code I already tired: var nameOfProperty = "id"; var propertyInfo = myObject.GetType().GetProperty(nameOfProperty); var id = jsonObject.id.ToString(); var id = Convert.ToString(jsonObject["id"]);
33 Replies
Angius
Angius•2y ago
Jesus Christ, dynamic, why Don't use dynamic
nael4slayer
nael4slayer•2y ago
long story my dude
Angius
Angius•2y ago
If someone tells you to use it, punch them in the face
nael4slayer
nael4slayer•2y ago
looong story well, it all started since I realized that the beloved attribute [FromForm] is not supported for the minimal-api and I'm doing an exercise involving authentication and authorization where I have an HttpClient that should post id, password as Url Form Encoded.
Angius
Angius•2y ago
So you know what gets posted Why dynamic, then
nael4slayer
nael4slayer•2y ago
That's why I need that attribute so badly
Angius
Angius•2y ago
And can't you just post JSON with your HttpClient?
nael4slayer
nael4slayer•2y ago
let me show you what I mean.
Angius
Angius•2y ago
Also, you can mix regular ol' controllers into your minimal API
nael4slayer
nael4slayer•2y ago
the login() from the client should do something like this:
Angius
Angius•2y ago
But why x-form-urlencoded tho You can just post JSON And receive JSON
nael4slayer
nael4slayer•2y ago
The point is not to only get it working. I wanna do it the right way with the [FromForm]. but unfortunately, that attribute is still not supported for the minimal api. I do hope that you understand me on this 🙂
Angius
Angius•2y ago
nael4slayer
nael4slayer•2y ago
What do you think ? lol
Angius
Angius•2y ago
What made you believe it's the right way? It's an API You communicate with it with HttpClient Not with an actual form Nothing here has any business to send or receive form data You're shooting yourself in the foot, and trying to tend to the wound by shooting it again
nael4slayer
nael4slayer•2y ago
Hmmm. Can you send a code you'd recommend for the Login from the client-side ?. I'm so confused and lost 😦
Angius
Angius•2y ago
Just... post JSON?
nael4slayer
nael4slayer•2y ago
So it doesn't need to be in that weird Url encoded form ?
Angius
Angius•2y ago
No
nael4slayer
nael4slayer•2y ago
thanks god
Angius
Angius•2y ago
Nothing here even remotely requires using it
nael4slayer
nael4slayer•2y ago
well. apparently I've been hitting my head with the wall all day for misunderstanding that point
Angius
Angius•2y ago
public enum GrantType
{
Password,
// ...
}

public sealed record LoginData(GrantType GrantType, string Login, string Password);
public enum GrantType
{
Password,
// ...
}

public sealed record LoginData(GrantType GrantType, string Login, string Password);
[HttpPost("Login")]
public async Task<IActionResult> Login(LoginData data)
{
// ...
}
[HttpPost("Login")]
public async Task<IActionResult> Login(LoginData data)
{
// ...
}
var data = new LoginData( ... );
var res = await _httpClient.PostAsJsonAsync("url", data);
var data = new LoginData( ... );
var res = await _httpClient.PostAsJsonAsync("url", data);
nael4slayer
nael4slayer•2y ago
Seems very reasonable. though I'm not quite familiar with the use of enum in this context. Can you tell me anything to clarify that part ?
Angius
Angius•2y ago
It's just an enum, not sure what more to say about it It's here to prevent the use of magic strings If something can be one of multiple things it's usually a good idea to make it an enum, instead of a, say, string What if you misspell "password" as "passwrd"? It'll be valid, but your code won't work
nael4slayer
nael4slayer•2y ago
So it just won't be compiled if I write passwd instead of password ?
Angius
Angius•2y ago
GrantType only has a member .Password So, yes, you'll only be able to use the types defined on the enum You won't be able to use GrantType.Agyuigwefgsjdkfgfasufkg unless .Agyuigwefgsjdkfgfasufkg is defined in GrantType enum
nael4slayer
nael4slayer•2y ago
Oh, I get it now.
Angius
Angius•2y ago
Meanwhile, "Agyuigwefgsjdkfgfasufkg" is a perfectly valid string
nael4slayer
nael4slayer•2y ago
I'm really thinking of removing this post before other people read dynamic so they'd come here and start bullying and yelling just like the usual. not everyone has a bit of patience
Angius
Angius•2y ago
As long as you understand the error of your ways, it's all good lol
nael4slayer
nael4slayer•2y ago
Nice.
And thanks a lot again
Angius
Angius•2y ago
👌