1

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

1 Answer 1

2

Try this,

[HttpGet, Route("api/testelements/{searchString}")]
public IEnumerable<TestElement> Get(string searchString)
{
    return Array.FindAll(testelements, s => s.name.Contains(searchString));
}


search(term: string): Observable<testelement[]> {
    return this.http
               .get(`api/testelements/` + ${term}) //append your search term at end of your Url.
               .map(response => response.json() as testelement[]);
}

Url to test from browser: http:yourUrl/api/testelements/123

Update

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

If you don't want to override the above method, then you need to change the Attribute route of this function,

[HttpGet, Route("api/test/{searchString}")] // previous route [HttpGet, Route("api/testelements/{searchString}")]   
public IEnumerable<TestElement> Get(string searchString)
{
    return Array.FindAll(testelements, s => s.name.Contains(searchString));
}

If you need more info on routing, Check this out

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

3 Comments

I'm getting a syntax error for ${term} argument of type search '{term: string}' is not assignable of type 'RequestOptionArgs' Object literal may specify known parameters are you sure this is what you used?
Actually got it, it just needed + '${term}' instead of + ${term}. im going to edit your answer. thank you for your help!!!
one problem is that it also overrides this function... public IHttpActionResult Get(int id) { var testelement = testelements.FirstOrDefault((p) => p.id == id); if (testelement == null) { return NotFound(); } return Ok(testelement); }

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.