My problem is that the first Get method is called from this http request: http://localhost:56690/api/testelements/?name=aeg
wouldn't I expect my this to hit the second (string overload) method since it has a string parameter? How do I make it hit the second (string overload) method?
this is my web api controller in c#
// GET api/<controller>
public IEnumerable<TestElement> Get()
{
return testelements;
}
// GET api/<controller>/searchString
//[HttpGet, Route("api/testelements/{name=name}")]
public IEnumerable<TestElement> Get(string searchString)
{
return Array.FindAll(testelements, s => s.name.Contains(searchString));
}
for my angular service i have this typescript code
search(term: string): Observable<testelement[]> {
return this.http
.get(`api/testelements/?name=${term}`)
.map(response => response.json() as testelement[]);
}
my WepApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I dont want it to override this function!:
public IHttpActionResult Get(int id)
{
var testelement = testelements.FirstOrDefault((p) => p.id == id);
if (testelement == null)
{
return NotFound();
}
return Ok(testelement);
}