MVC4 RC的WebAPI参数绑定 [英] MVC4 RC WebApi parameter binding

查看:133
本文介绍了MVC4 RC的WebAPI参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MVC4公测RC和最新autofac升级。下面的行动是正确的约束力,但现在这两个参数都为空。我看到他们改变有关格式化程序之类的事,但我不知道是什么原因导致我的问题

  [HttpPost]
RedirectModel MyAction(字符串值1,字符串值1)

请求

 方法:POST
接受:应用/ JSON
网址:API /控制器/ myaction
BODY:{值1:1000,值2:富}


解决方案

真的不知道为什么从Beta的变化,但我可以使它通过改变操作签名的工作:

  [HttpPost]
RedirectModel MyAction(MyActionDTO DTO)

和定义MyActionDTO为

 公共类MyActionDTO
 {
        公共字符串值1 {搞定;组; }
        公共字符串值2 {搞定;组; }
 }

有人乱扔不​​能够结合使用两个字符串PARAMATERS全身多个参数异常。我想用DTO更加紧密地反对重新presents你在AJAX调用(JS​​ON对象)发送的。

I upgraded from MVC4 beta to RC and the latest autofac. The following action was binding properly, but now both parameters are null. I see they changed things about the Formatters and such but I am not sure what caused my problem

[HttpPost]    
RedirectModel MyAction(string value1, string value1)

REQUEST

Method: POST
Accept: application/json
URL: api/controller/myaction
BODY: {"value1":"1000", "value2":"foo"}

解决方案

Not really sure why the change from Beta, but I was able to make it work by changing the action signature to:

[HttpPost]    
RedirectModel MyAction(MyActionDTO dto)

and defining MyActionDTO as

 public class MyActionDTO 
 {
        public string value1 { get; set; }
        public string value2 { get; set; }
 }

It was throwing an exception about not being able to bind to multiple body parameters using the two string paramaters. I guess using the DTO object more closely represents what you're sending in the AJAX call (a JSON object).

这篇关于MVC4 RC的WebAPI参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆