c# - MVC5 WebApi 2 create not getting value from body ([FromBody]) -


i created simple create web api 2 action object post body , set dal layer. no matter using postman object method, stays null.

the model looks this:

namespace webapi.models {     using system;     using system.collections.generic;      public partial class classes     {         public int id { get; set; }         public string classname { get; set; }         public int maxstudents { get; set; }     } } 

my controller follows:

[httppost] public ihttpactionresult createclass([frombody] classes classobj) {     if (classobj == null)     {         return badrequest("missing parameters.");     }      var newclass = new classes()     {         classname = classobj.classname,         maxstudents = classobj.maxstudents     };      _context.classes.add(newclass);     _context.savechanges();      var newclassurl = url.content("~/") + "/api/classes/";      return created(newclassurl, newclass); } 

now when use postman tried 2 options.

option 1:

url: http://localhost:53308/api/classes/ headers: content-type: applications/json  [   "classobj":  {        classname = "test"        maxstudents = 100   } ] 

option 2:

url: http://localhost:53308/api/classes/ headers: content-type: applications/json         classname = "test"        maxstudents = 100 

but in both cases classobj stays empty , returns "missing parameters.". i'am missing here.

what doing wrong?

your payloads not match expectation of action.

for example

[httppost] public ihttpactionresult createclass([frombody] classes classobj) {     //... } 

would expect json data looks this

{     "classname": "test"     "maxstudents": 100 } 

also given model posted action same type added store there isn't need create new instance.

[httppost] public ihttpactionresult createclass([frombody] classes classobj) {     if (classobj == null) {         return badrequest("missing parameters.");     }      _context.classes.add(classobj);     _context.savechanges();      var newclassurl = url.content("~/") + "/api/classes/" + classobj.id.tostring();      return created(newclassurl, classobj); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -