2

我是微软 MVC 的新手,所以请原谅我的新手问题。我们正在将 Ruby/Rails 应用程序移植到 MS MVC 4。随着时间的推移,我们将这样做,因此 .NET MVC 必须能够在不修改的情况下使用 Ruby/Rails api 请求。我无法将 POST 正文中的 xml 反序列化为对象。当我在返回行上放置一个断点时,登录的值为空。任何帮助将不胜感激。

API 请求经过身份验证...这是来自客户端的请求。

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>

这是 Api 控制器

public class UsersController : ApiController
{
    private IUserRepository repository;

    public UsersController(IUserRepository repo)
    {
        repository = repo;
    }

    [HttpPost]
    public AuthenticateUserViewModel authenticate([FromBody]AuthenticateUserViewModel login)
    {
        return null;
    }
}

这是参数类

[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; }
}

这是 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"));
    }
}
4

0 回答 0