2

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 Answer 1

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:

Sign up to request clarification or add additional context in comments.

5 Comments

As described in question, customer code can contain /, * , % and other characters which are not allowed as url directory names. In windows kernel.sys immediately returns bad request error for this and does not pass it to IIS. How to use RESTful desing in this case for last API link sample in your answer ?
If the customer code can contain such dangerous characters then they do not belong to the path portion of the url. They should be passed used as query string parameters. Leave them as query strings and go with the approach illustrated in my answer or choose a better property to identify your customers than this code.
I tried this but search is always null. I updated question. How to fix?
I posted this as separate question in stackoverflow.com/questions/20180068/…
Apparently you should decorate the model with the [FromUri] attribute. I have updated my answer to reflect that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.