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
Jesus Christ,
dynamic
, why
Don't use dynamic
long story my dude
If someone tells you to use it, punch them in the face
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.
So you know what gets posted
Why
dynamic
, thenThat's why I need that attribute so badly
And can't you just post JSON with your
HttpClient
?let me show you what I mean.
Also, you can mix regular ol' controllers into your minimal API
the login() from the client should do something like this:
But why
x-form-urlencoded
tho
You can just post JSON
And receive JSONThe 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 🙂
What do you think ?
lol
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
Hmmm.
Can you send a code you'd recommend for the Login from the client-side ?.
I'm so confused and lost 😦
Just... post JSON?
So it doesn't need to be in that weird Url encoded form ?
No
thanks god
Nothing here even remotely requires using it
well. apparently I've been hitting my head with the wall all day for misunderstanding that point
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 ?
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 workSo it just won't be compiled if I write passwd instead of password ?
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
enumOh, I get it now.
Meanwhile,
"Agyuigwefgsjdkfgfasufkg"
is a perfectly valid stringI'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
As long as you understand the error of your ways, it's all good lol
Nice.
And thanks a lot again
And thanks a lot again
👌