Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate Web API for request with code and term query parameters
    text
    copied!<p>In ASP.NET MVC4 application Json Web api needs to be created to serve requests with term and code url parameters like:</p> <p><a href="http://myapp.com/api/customers" rel="nofollow">http://myapp.com/api/customers</a></p> <p>returns all customers</p> <p><a href="http://myapp.com/api/customers?term=partofname" rel="nofollow">http://myapp.com/api/customers?term=partofname</a></p> <p>returns 0 .. n customers</p> <p>and</p> <p><a href="http://myapp.com/api/customers?code=customercode" rel="nofollow">http://myapp.com/api/customers?code=customercode</a></p> <p>returns 1 customer always</p> <p>code is customer id which can contain / and other characters which are not allowed in url passed to windows http server by http.sys in windows kernel</p> <p>API controller below is tried but it causes compile error</p> <p>Error Type 'Erp.Controllers.CustomersController' already defines a member called 'Get' with the same parameter types.</p> <p>How to fix this ? Which is proper way to create API class for such request? Should odata or different method names used or other way? Application must run in Windows 2003 server and in Mono so Web API v.2 cannot used.</p> <p>Method and query string parameter names can changed if this helps. Returned data format cannot changed.</p> <pre><code> public class CustomersController : ApiController { public object Get() { var res = GetAllCustomers(); return Request.CreateResponse(HttpStatusCode.OK, new { customers = res.ToArray() } ); } public object Get(string term) { var res = GetCustomersByTerm(term); return Request.CreateResponse(HttpStatusCode.OK, new { customers = res.ToArray() } ); } public object Get(string code) { var res = GetCustomersById(code); // code is actually unique customer id which can contain / and other characters which are not allowed in // url directory names in windows http server return Request.CreateResponse(HttpStatusCode.OK, new { customers = res.ToArray() }); } } </code></pre> <p>default routing is used:</p> <pre><code> config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); </code></pre> <p><strong>Update</strong></p> <p>I tried answer but search parameter is always null. Whole request is below. How to pass parameters ?</p> <pre><code>GET /api/customers?term=kaks&amp;_=1385320904347 HTTP/1.1 Host: localhost:52216 Connection: keep-alive Accept: application/json, text/javascript, */*; q=0.01 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 Referer: http://localhost:52216/erp/Sale Accept-Encoding: gzip,deflate,sdch Accept-Language: et-EE,et;q=0.8,en-US;q=0.6,en;q=0.4 Cookie: .myAuth=8B6B3CFFF3DF64EBEF3D258240D217C56603AF255C869FBB7934560D9F560659342DC4D1EAE6AB28454122A86C3CE6C598FB594E8DC84A; My_Session=5aw2bsjp4i4a5vxtekz </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload