Search(string...">

Asp.net web api binding list options - model

In my controller, I:

[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 make a binding to the model. This is easy for primitive types, but what if I have a list of primitive types?

I did this in Global.asax:

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

But I do not know what to set for lists ...

I found on some site that I can add [ModelBinder] in front of the list, but when I do this, I just get a red underline on that word.

Does anyone have an idea how to do this?

+4
source share
1 answer

From your description, it looks like you found this article or its kind of like

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

We recommend using the ModelBinder attribute. I still recommend this method if you can make it work. The red underline that you are describing seems like you may not have the appropriate links. Make sure you have the appropriate links in your class to access this attribute, in which case it looks like System.Web.Http.ModelBinding

http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.modelbinderattribute(v = vs .118) .aspx

If this fails, you probably cannot use model binding. From the first article

Web API only uses model binding for "simple types"

you can also use JSON Formatter or the like, it is not difficult and will easily support List structures with well-formatted JSON.

here is a great introductory article for using this

http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx

+5
source

Source: https://habr.com/ru/post/1490761/


All Articles