Custom JSON structure in .NET Core -


i'm trying learn .net core; background largely php absence of type system makes i'm trying here trivial.

i'm trying create api small wrapper response. instead of sending {"user": "..."} can send {"message": "...", "data": {"user": "..."}}

to start, began poco wrapper:

public class appresponse<t> {     public string message { get; set; }     public t data { get; set; } } 

then, instantiate wrapper differently each time need response object:

[httppost] public async task<iactionresult> register(registerviewmodel model) {     if (modelstate.isvalid)     {         var user = new applicationuser { username = model.email, email = model.email };         var result = await _usermanager.createasync(user, model.password);          if (result.succeeded)         {             var okresponse = new appresponse<registerviewmodel>();             okresponse.message = "your account has been created.";             return ok(okresponse);         }         else         {             var errorresponse = new appresponse<ienumerable<identityerror>>();             errorresponse.data = result.errors;             return badrequest(errorresponse);         }     }     var badrequest = new appresponse<registerviewmodel>();     badrequest.data = model;      return badrequest(badrequest); } 

is there easier/better way in .net core? intend web api, ability make more complicated json responses (for things such pagination, validation errors, etc.) crucial.

i apologize confusion of camel case in variables, i'm still figuring out best practices in c#.

edit: have simplified code use more restful approach. currently:

public async task<iactionresult> register(registerviewmodel model) {     if (modelstate.isvalid)     {         var user = new applicationuser { username = model.email, email = model.email };         var result = await _usermanager.createasync(user, model.password);          if (result.succeeded)         {             return ok(user);         }         return badrequest(result);     }      // if got far, failed.     return badrequest(new     {         errors = modelstate     }); } 


Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -