I am new to microsoft's MVC so please forgive my newbie question. We are porting a Ruby/Rails application to MS MVC 4. We will be doing this over time so the .NET MVC must be able to consume Ruby/Rails api request without modification. I am having trouble deserializing the xml in the POST body to an object. When I place a break point on the return line the value of login is null. Any help would be greatly appreciated.
The API request is authenticate...here is the request from the client.
POST http://cloudcare.webservices.local/users/authenticate HTTP/1.1
Host: cloudcare.webservices.local
Connection: keep-alive
Content-Length: 132
Accept: text/xml
<authenticate>
<login>david</login>
<password>secure</password>
<user_type_id>2</user_type_id>
</authenticate>
Here is the Api Controller
public class UsersController : ApiController
{
private IUserRepository repository;
public UsersController(IUserRepository repo)
{
repository = repo;
}
[HttpPost]
public AuthenticateUserViewModel authenticate([FromBody]AuthenticateUserViewModel login)
{
return null;
}
}
Here is the parameter class
[Serializable]
[DataContract(Name = "authenticate")]
public class AuthenticateUserViewModel
{
[DataMember(Name = "login")]
public string Login { set; get; }
[DataMember(Name = "password")]
public string Password { get; set; }
[DataMember(Name = "user_type_id")]
public int UserTypeId { get; set; }
}
Here is the WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { action = "index", id = RouteParameter.Optional}
);
var unity = new UnityContainer();
unity.RegisterType<Controllers.UsersController>();
unity.RegisterType<Repository.IUserRepository, Repository.Repositories.UserRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new IoCContainer(unity);
config.EnableSystemDiagnosticsTracing();
config.Formatters.XmlFormatter.UseXmlSerializer = true;
config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
}
}