Create Web API for request with code and term query parameters

安稳与你 提交于 2019-12-22 18:12:40

问题


In ASP.NET MVC4 application Json Web api needs to be created to serve requests with term and code url parameters like:

http://myapp.com/api/customers

returns all customers

http://myapp.com/api/customers?term=partofname

returns 0 .. n customers

and

http://myapp.com/api/customers?code=customercode

returns 1 customer always

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

API controller below is tried but it causes compile error

Error Type 'Erp.Controllers.CustomersController' already defines a member called 'Get' with the same parameter types.

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.

Method and query string parameter names can changed if this helps. Returned data format cannot changed.

 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() });
        }
}

default routing is used:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

Update

I tried answer but search parameter is always null. Whole request is below. How to pass parameters ?

GET /api/customers?term=kaks&_=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

回答1:


You could define a view model:

public class SearchViewModel
{
    public string Term { get; set; }
    public string Code { get; set; }
}

and then group the 2 operations in a single one:

public HttpResponseMessage Get([FromUri] SearchViewModel search)
{
    if (!string.IsNullOrEmpty(search.Code))
    {
        var customer = GetCustomersById(search.Code);
        return Request.CreateResponse(HttpStatusCode.OK, customer);
    }

    var customers = GetCustomersByTerm(search.Term).ToArray();
    return Request.CreateResponse(HttpStatusCode.OK, customers);
}

But personally I would go with a more RESTful design:

  • http://myapp.com/api/customers - return all customers
  • http://myapp.com/api/customers?term=partofname - return all customers with the specified term
  • http://myapp.com/api/customers/some_code - return a single customer with the specified code


来源:https://stackoverflow.com/questions/20176422/create-web-api-for-request-with-code-and-term-query-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!