Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a List<string> to an MVC Web API method using the browser bar

Tags:

I have an MVC Web API Get method that accepts a List<string> as a parameter. I'm trying to access this method using simply the browser bar. How is this done? Using ../APIName?parameter1=value1&parameter2=value2&... passes a single parameter between two ampersands as opposed to a list.

like image 428
Mark13426 Avatar asked Dec 06 '12 03:12

Mark13426


People also ask

Can we use MVC with Web API?

You can mix Web API and MVC controller in a single project to handle advanced AJAX requests which may return data in JSON, XML or any others format and building a full-blown HTTP service. Typically, this will be called Web API self-hosting.

How do I pass multiple objects to Web API?

Best way to pass multiple complex object to webapi services is by using tuple other than dynamic, json string, custom class. No need to serialize and deserialize passing object while using tuple. If you want to send more than seven complex object create internal tuple object for last tuple argument.


1 Answers

  1. Make sure your parameter of your action method is marked as [FromUri]. By default the value is expected to be passed from the body of the request since it is a complex type.

    public List<string> Get([FromUri] List<string> parameter) {...} 
  2. The query string parameter should be of this format .../APIName?parameter[]=value1&parameter[]=value2&....

Hope this helps.

like image 147
Maggie Ying Avatar answered Sep 21 '22 08:09

Maggie Ying