✅ Web API Controller Response JObject
i have problem with the response in my Controller. there is no value in the response only brackets
48 Replies
If the controller is yours, you need to provide the code where you're populating the response payload
IsSuccess
suggests a boolean, no idea why anything other would be used{{
"IsSuccess": true,
"Message": "Success",
"Data": {
"id": 1,
"name": "Philip2",
"weight": 105.0,
"height": 34.5,
"birth_date": "2009-11-08T14:40:53",
"age": 15,
"BMI": 1.5217391304347827
}
}}
these are the values when i debug
Why is there
JObject
Why not an actual modelits just the same
let me rephrase my code
look bro, i replace it with string return
ublic async Task<ActionResult<string>> GetUserProfile([
see that string
"string"
Yes
if i will replace that with Jobject. that cause a problem
Well, hopefully someone can help with using
JObject
as a reponse (what is DefaultReponse
?) as I've never seen it being used in a controlleroh defaultResponse is just a class
Yes I know
i created it
Is there a specific reason you are using JObject? Most folks don't use that
Literally no one ever
hahah really?
Asp.net controllers automatically handle json serialization for you
oh i see
You can just make your controller method return a
Task<YourDTO>
and it'll serialize by default using System.Text.Jsonok let me try.
ah. i want to use dynamic object that is why
Why?
That's also extremely atypical
try this...
wow. nice
Was about to say, haha
you dont like JObject guys
C# is a strongly typed language
hey bro what is this "T"
you can then create specific DTOs for each response..
And return it like this (crude example)...
Generic classes and methods - C#
Learn about generics. Generic types maximize code reuse, type safety, and performance, and are commonly used to create collection classes.
Beat me to it
what the heckk guys
You missed a bit further up
DefaultResponse<T>
in the class definitionwow. thanks @Kyr
Generics can be very handy... I recommend reading up on them. The link @PixxelKick posted above is a good start
Thanks
wow @Kyr
i need to replace a lot bro
from my model to methods
oh my
but guys, just wondering why JObject is not working?
GUYS
its working
JObject
hahahhaha
builder.Services.AddControllers().AddNewtonsoftJson();
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
@philip b What version of .NET are you working in?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
8
but it is much more easier to use
and dynamically. i think
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
i dont have bro. only in my local
but i'll try to push it in github
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View