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

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

问题描述

我从 MVC4 beta 升级到 RC 和最新的 autofac.以下操作已正确绑定,但现在两个参数都为空.我看到他们改变了有关格式化程序等的内容,但我不确定是什么导致了我的问题

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)

请求

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

推荐答案

不太确定为什么要从 Beta 更改,但我能够通过将操作签名更改为:

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)

并将 MyActionDTO 定义为

and defining MyActionDTO as

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

它抛出了一个关于无法使用两个字符串参数绑定到多个主体参数的异常.我想使用 DTO 对象更能代表您在 AJAX 调用中发送的内容(JSON 对象).

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天全站免登陆