json - Web API - Cannot de-serialize request object -


i trying create quick application try learn angularjs web api (i have worked asp.net mvc not had chance use web api) server-side, cannot seem object serialize when posting the web api method.

my object simple user object inherits baseentity object:

 public class user : baseentity {     /// <summary>     /// gets or sets name.     /// </summary>     /// <value>     /// name.     /// </value>     public string name { get; set; }     /// <summary>     /// gets or sets email.     /// </summary>     /// <value>     /// email.     /// </value>     public string email { get; set; }      /// <summary>     /// gets or sets password.     /// </summary>     /// <value>     /// password.     /// </value>     public string password { get; set; }  }  public class baseentity : mongorepository.entity, interfaces.ientity     {         /// <summary>         /// gets or sets id object (the primary record entity).         /// </summary>         /// <value>         /// id object (the primary record entity).         /// </value>         public new int id { get; set; }         /// <summary>         /// gets or sets value indicating whether <see cref="baseentity"/> selected.         /// </summary>         /// <value>         ///   <c>true</c> if selected; otherwise, <c>false</c>.         /// </value>         public bool selected { get; set; }         /// <summary>         /// gets or sets value indicating whether <see cref="baseentity"/> deleted.         /// </summary>         /// <value>         ///   <c>true</c> if deleted; otherwise, <c>false</c>.         /// </value>         public bool deleted { get; set; }     } 

my latest method attempt follows:

 public httpresponsemessage post(jobject user)     {         user userobject = user.toobject<user>();         _repo.add(userobject);          return request.createresponse(httpstatuscode.created, "user created");     } 

my angularjs snippet clarity on being posted ui (the properties set using ng-model , can see them being passed via fiddler, web api method de-serializes these properties null rather ""):

      $scope.user = {       name: '',       password: '',       email: '',   }   $scope.register = function () {           $http({               method: 'post',               url: '/api/user/',               data: $scope.user //also tried json.stringify($scope.user)           })             .success(function () {                 //handle successful registration             })             .error(function () {                 //show error message             });   } 

when debugging, can see user parameter has been populated object. have tried setting properties during request, 1 property etc , have called api via postman , application, ever way use, blank user object when try parse user paramter object user object. same when put parameter type user object.

i have looked around , examples , answered questions don't inherit anything, , have tried different content-type's thinking may problem (this when had user object type , parameter type method).

is there quirk missing json.net/built in web api deserialization when comes objects derived other classes, or there missing?

edit: have removed inheritance object , binds expect, not sure why doesn't work when there kind of inheritance. there setup required parameter binding work when object inherits another?:

public class user {     public string name { get; set; }      public string email { get; set; }      public string password { get; set; }  } 

i thik webapi clever enough bind directly typed object. method can take user type , not jobject.

also, make sure property names in javascript match property names of model well. or else hydrate model passed in default values, dangerous booleans!

it routing. don't forget default route in webapi includes '/api/' ("api/{controller}/{id}"), doesn't in mvc?

without little more detail. can say.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -