C
C#5mo ago
douchebag

✅ Web API Controller Response JObject

i have problem with the response in my Controller. there is no value in the response only brackets
No description
48 Replies
SG97
SG975mo ago
If the controller is yours, you need to provide the code where you're populating the response payload
douchebag
douchebagOP5mo ago
No description
SG97
SG975mo ago
IsSuccess suggests a boolean, no idea why anything other would be used
douchebag
douchebagOP5mo ago
{{ "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
SG97
SG975mo ago
Why is there JObject Why not an actual model
douchebag
douchebagOP5mo ago
its just the same let me rephrase my code
douchebag
douchebagOP5mo ago
look bro, i replace it with string return
No description
douchebag
douchebagOP5mo ago
ublic async Task<ActionResult<string>> GetUserProfile([ see that string "string"
SG97
SG975mo ago
Yes
douchebag
douchebagOP5mo ago
if i will replace that with Jobject. that cause a problem
SG97
SG975mo ago
Well, hopefully someone can help with using JObject as a reponse (what is DefaultReponse?) as I've never seen it being used in a controller
douchebag
douchebagOP5mo ago
oh defaultResponse is just a class
SG97
SG975mo ago
Yes I know
douchebag
douchebagOP5mo ago
i created it
PixxelKick
PixxelKick5mo ago
Is there a specific reason you are using JObject? Most folks don't use that
SG97
SG975mo ago
Literally no one ever
douchebag
douchebagOP5mo ago
hahah really?
PixxelKick
PixxelKick5mo ago
Asp.net controllers automatically handle json serialization for you
douchebag
douchebagOP5mo ago
oh i see
PixxelKick
PixxelKick5mo ago
You can just make your controller method return a Task<YourDTO> and it'll serialize by default using System.Text.Json
douchebag
douchebagOP5mo ago
ok let me try. ah. i want to use dynamic object that is why
douchebag
douchebagOP5mo ago
No description
PixxelKick
PixxelKick5mo ago
Why? That's also extremely atypical
Kyr
Kyr5mo ago
try this...
public class DefaultResponse<T>
{
public bool isSuccess { get; set; }
public string message { get; set; }
public T data { get; set; }
}
public class DefaultResponse<T>
{
public bool isSuccess { get; set; }
public string message { get; set; }
public T data { get; set; }
}
douchebag
douchebagOP5mo ago
wow. nice
PixxelKick
PixxelKick5mo ago
Was about to say, haha
douchebag
douchebagOP5mo ago
you dont like JObject guys
PixxelKick
PixxelKick5mo ago
C# is a strongly typed language
douchebag
douchebagOP5mo ago
hey bro what is this "T"
Kyr
Kyr5mo ago
you can then create specific DTOs for each response..
public class MyCustomResponse
{
public int Id { get; set; }
// etc...
}
public class MyCustomResponse
{
public int Id { get; set; }
// etc...
}
And return it like this (crude example)...
return new DefaultResponse<MyCustomResponse> { isSucess = true, data = new MyCustomResponse{ Id = 1 } };
return new DefaultResponse<MyCustomResponse> { isSucess = true, data = new MyCustomResponse{ Id = 1 } };
PixxelKick
PixxelKick5mo ago
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.
Kyr
Kyr5mo ago
Beat me to it
douchebag
douchebagOP5mo ago
what the heckk guys
No description
Kyr
Kyr5mo ago
You missed a bit further up DefaultResponse<T> in the class definition
douchebag
douchebagOP5mo ago
wow. thanks @Kyr
Kyr
Kyr5mo ago
Generics can be very handy... I recommend reading up on them. The link @PixxelKick posted above is a good start
douchebag
douchebagOP5mo ago
Thanks
douchebag
douchebagOP5mo ago
wow @Kyr
No description
douchebag
douchebagOP5mo ago
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
douchebag
douchebagOP5mo ago
No description
douchebag
douchebagOP5mo ago
builder.Services.AddControllers().AddNewtonsoftJson();
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Kyr
Kyr5mo ago
@philip b What version of .NET are you working in?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
douchebag
douchebagOP5mo ago
8 but it is much more easier to use and dynamically. i think
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
douchebag
douchebagOP5mo ago
i dont have bro. only in my local but i'll try to push it in github
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server