2

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"));
    }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
  • For starter: you can try to set the Content-Type in your request to `text/xml` and also removing the `config.Formatters.XmlFormatter.UseXmlSerializer = true;` because as far as I know the XmlSerializer does not understand the `DataMember` attributes so it will have problems with the different casing Login vs login – nemesv Mar 13 '13 at 22:06
  • Thanks for you reply, but I have tried the content-type change and did not work. Also, I did try the uppercase login and it did not work either. – user2162995 Mar 14 '13 at 10:22
  • I changed the dataContract and dataMember attributes to XmlRoot and XmlElement attributes and that worked. Thanks for the tip. – user2162995 Mar 14 '13 at 10:30

0 Answers0