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