ASP.NET Web API 控制器无法处理动态对象?
在我的代码中,我有一个基类 Foo,我的所有对象都继承自 Foo 对象。 假设我有一个这样的类,
public class Bar : Foo {
public string Heading { get;set; }
}
我尝试将 ApiControllers put 方法与动态一起使用,但出现此错误 http: //paste2.org/p/1914054
这是我在 ApiController 中使用的代码
public void Put(string id, dynamic model) {
//do stuff
}
如果我使用普通控制器,我可以使用动态来发布数据。是否可以添加使 api 控制器动态工作,或者我是否需要构建自己的模型绑定器?
看起来有些人认为即使在 MVC 3 中输入参数也不能是动态的,但事实并非如此,这就是我问这个问题的原因。 这个MVC 3 中的控制器使用动态作为输入参数效果非常好。
In my code I have a base class Foo and all my objects inherits from the Foo object.
So let's say I have a class like this
public class Bar : Foo {
public string Heading { get;set; }
}
I have tried to use the ApiControllers put method with dynamic but I get this error http://paste2.org/p/1914054
This is the code I'm using in the ApiController
public void Put(string id, dynamic model) {
//do stuff
}
If I use a normal controller I can use dynamic to post data. Is it possible to add make the api controller work with dynamic or do I need to build my own model binder?
It sees like some thinks that even in MVC 3 the input parameters can't be a dynamic but that is not true and that's why I ask this question. This controller in MVC 3 works just great with dynamic as input parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我可以使用 ASP.NET Web API 方法来完成此操作:
使用 JSON 数组:
并且它有效......
Hm, I was able to just do this with an ASP.NET Web API method:
using a JSON array:
And it worked....
我想是的。让我们看一下提供的示例:
您能告诉我这个
editorModel
动态变量在这个控制器操作中如何使用/在哪里使用吗?为了进一步简化这个控制器操作,它是有效的,因为它从不使用作为参数传递的动态变量。我对其进行了简化,以更好地说明此操作在模型绑定方面的大致作用(当然,丢弃我们在这里不感兴趣的所有基础设施噪音来说明问题):
在此操作中,
TryUpdateModel
UpdateModel
方法在_model
实例变量上调用,该变量在构造函数中传递,类型为IPageModel
。 ASP.NET MVC 不可能知道(当然没有自定义模型绑定器)动态操作参数的类型。只需运行此代码,在Update
操作中放置一个断点,并观察editorModel
变量的类型。它只是System.Object
。没有奇迹。所以对我来说,这在 ASP.NET Web API 中的工作原理是完全正常的。
I think so. Let's take a look at the provided example:
Can you point me how/where exactly is this
editorModel
dynamic variable used inside this controller action?And to even further simplify this controller action, it works, because it never never uses the dynamic variable passed as argument. I have simplified it to better illustrate what this action is roughly doing concerning model binding (throwing away of course all the infrastructure noise that we are not interested in here to illustrate the problem):
Inside this action the
TryUpdateModel
andUpdateModel
methods are called on the_model
instance variable which is passed in the constructor and is of typeIPageModel
. ASP.NET MVC cannot possibly know (without a custom model binder of course) the type of your dynamic action argument. Just run this code, put a breakpoint inside theUpdate
action and observe the type of theeditorModel
variable. It will simply beSystem.Object
. There are no miracles.So it's for me it's perfectly normal that this works the same in ASP.NET Web API.
http://www.binaryintellect.net/articles/589f6915-f4c0-4bf9-9f94-4d00bd445c16.aspx
这个解决方案工作得很好。
我有一个 HTML 表单,其中所有输入控件都是动态的。很难将表单数据传输到 MVC 控制器,然后再传输到 Web Api 2 控制器。
您的服务器端 Web api 方法需要像
FormDataCollection 一样驻留在 System.Net.Http.Formatting 命名空间中。
如果您直接从网页(jquery)提供数据,那么上面的链接解决方案可以正常工作。
如果您将数据网页发布到 MVC 页面(c# 代码),然后进一步发布到 Web api 方法,则代码如下所示:
浏览器端代码:
http://www.binaryintellect.net/articles/589f6915-f4c0-4bf9-9f94-4d00bd445c16.aspx
This solution works perfectly fine.
I had an HTML form where all the input controls were dynamic. Had a hard time plumbing the form data to MVC controller and then again to Web Api 2 controller.
Your server side web api method needs to be like
FormDataCollection resides in System.Net.Http.Formatting namespace.
If you are posing data directly from a web page (jquery), then the above link solution is works fine.
If you posting data web page to an MVC page (c# code) which is then further posted to a web api method, then the code looks like this:
The browser side code: