Create Web API for request with code and term query parameters

烈酒焚心 提交于 2019-12-06 12:39:15

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:

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