asp.net网页API - 模型绑定列表参数 [英] asp.net web api - model binding list parameter

查看:272
本文介绍了asp.net网页API - 模型绑定列表参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器我有:

[AcceptVerbs("GET", "POST")]
    public List<BuzzMonitor.Web.Message> Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds) 
    {...}

和我想要做的模型绑定。它很容易为​​基本类型,但是当我有原始类型的列表,该怎么办?

and I want to do model binding. It's easy for primitive types, but what to do when I have a list of primitive types?

我没有像这样在Global.asax中:

 GlobalConfiguration.Configuration.Routes.MapHttpRoute("SearchWithParameters", "api/{controller}/{action}/{text}/{dateFrom}/{dateTo}/?/?"

但我不知道该怎么对列表设置...

But I dont know what to set for lists...

我的一些网站,我可以列表前加[ModelBinder的]研究发现,但是当我这样做,我刚上车的那个词红色下划线。

I found on some site that I can add [ModelBinder] before list, but when I do that I just get red underline on that word.

有没有人有一个想法,该怎么办呢?

Does anyone have an idea how to do it?

推荐答案

从你的描述,它看起来像你已经发现这篇文章或者一个像它

From your description it looks like you have found this article or one like it

http://lostechies.com/keithdahlby/2012/10/04/asp-net-web-api-list-parameter-binding/

建议您使用ModelBinder的属性。我还是推荐这种方式,如果你能得到它的工作。您所描述的红色下划线听起来像你可能没有合适的引用。请确保你在你的类适当的引用来访问该属性,在这种情况下,它看起来像 System.Web.Http.ModelBinding

recommending you use the ModelBinder attribute. I still recommend this way if you can get it to work. The red underline you describe sounds like you may not have the appropriate references. Please make sure you have the appropriate references in your class to access that attribute, in this case it looks like System.Web.Http.ModelBinding

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.modelbinderattribute%28v=vs.118%29.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.modelbinderattribute(v=vs.118).aspx

如果失败了你可能不能够使用模型绑定。从第一篇文章

If that fails you will likely not be able to use Model Binding. From the first article

的Web API只使用型号为简单类型

您也可以看看使用JSON格式化或类似的,这并不困难,而且很容易支持列表结构,具有良好的格式JSON。

you can also look at using a JSON Formatter or similar, this is not difficult and would easily support List structures, with well formatted JSON.

在这里是一个伟大的介绍性的文章使用这种

here is a great introductory article to using this

<一个href=\"http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx\" rel=\"nofollow\">http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx

这篇关于asp.net网页API - 模型绑定列表参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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