C#从MVC控制器参数调用Web Api方法始终为null

时间:2014-04-30 15:05:18

标签: c# asp.net-mvc

我有一种情况,我有一个MVC控制器,我正在调用WebApi POST方法。我将DTO作为参数传递... WebApi方法被命中,但参数为null。我错过了什么?

谢谢! MVC控制器:

     [Authorize]
    public async Task<ActionResult> Index()
    {
        Permissions service = new Permissions();
        ViewBag.Title = "Deployment Manager";

        string uri = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiRestfulUrl"] + "/Permissions";
        using (var client = new HttpClient())
        {
            var permissions = new PermissionsDTO() { UserName = "rherhut" };
            var response = await client.PostAsJsonAsync(uri, permissions);
            if (response.IsSuccessStatusCode)
            {
                // Get response data how??
            }

        }

        return View();
    }

WebAPI POST方法:

     public HttpResponseMessage Post(HttpRequestMessage request, [FromBody]PermissionsDTO permissions)
    {
         var data = repository.HasAdminRights(permissions.UserName); // permissions.UserName is null
        var response = new ApiResponseCreator<PermissionsDTO>();
        return response.FormatReturnData(request, data);
    }

1 个答案:

答案 0 :(得分:0)

我通过使用“Serialize”属性修饰dto类解决了这个问题,并且由于某种原因这很有效。