1

Take a look at the code from this site: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio

In GET method one can see (code). Function filter over id: aip/Todo/5. How can I create a method (in order to use HttpGet) but to filter over another parameter like "name"?

// GET: api/Todo
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
{
    return await _context.TodoItems.ToListAsync();
}

// GET: api/Todo/5
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
    var todoItem = await _context.TodoItems.FindAsync(id);

    if (todoItem == null)
    {
        return NotFound();
    }

    return todoItem;
}

In the example we have:

namespace TodoApi.Models
{
    public class TodoItem
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public bool IsComplete { get; set; }
    }
}

So what I want I want to have 3 functions: // GET: api/Todo/5 [HttpGet("{id}")]

one to filter over Id, another one to filter over Name and another one to filter over IsComplete.

How can I do that?

PabloPhones
  • 123
  • 1
  • 1
  • 9

2 Answers2

1

I found answer in other post: Single controller with multiple GET methods in ASP.NET Web API and here: https://nodogmablog.bryanhogan.net/2016/10/web-api-2-controller-with-multiple-get-methods/

I guess I was not specific what I want. But thanks a lot for your answers H.Herzl.

PabloPhones
  • 123
  • 1
  • 1
  • 9
0

First at all, GetTodoItem method retrieves a todo item by ID, if you want to search todo items by name please keep on mind that search will return one or more items, in that case you should to use IEnumerable type instead of TodoItem; that search criteria applies only if name is unique.

Anyway, if you want to search by name you can edit you GetTodoItems method like this:

// GET: api/Todo

[HttpGet]
public async Task<IEnumerable<TodoItem>> GetTodoItems(string name = "")
{
    // Cast DbSet property to IQueryable<TModel>
    var query = _context.TodoItems.AsQueryable();

    // Validate if name parameter has value
    if (!string.IsNullOrEmpty(name))
        query = query.Where(item => item.Name.Contains(name));

    // Get results from database
    return await query.ToListAsync();
}

Also if you want to search in GetTodoItem by another parameters you can make these changes on your method:

// GET: api/Todo/5

[HttpGet("{id}")]
public async Task<IActionResult<TodoItem>> GetTodoItem(long id, string name = "")
{
    var todoItem = await _context.TodoItems.FirstOrDefaultAsync(item => item.id == id);

    if (!string.IsNullOrEmpty(name))
        todoItem = await _context.TodoItems.FirstOrDefaultAsync(item => item.Name.Contains(name));

    return todoItem;
}

If you want to know more about search in controllers, please check this article: Creating Web API in ASP.NET Core 2.0

Please let me know if this answer is useful.

H. Herzl
  • 3,030
  • 1
  • 14
  • 19
  • I want to have: GET: api/Todo - all Todo Items, api/Todo/5 - get Todo Item with Id == 5, and to these methods I want to have ability to make the same like api/Todo/5 but where I make filter over other parameters. – PabloPhones Jul 04 '19 at 10:23
  • If you're filtering by id, why do you need to filter by another parameters if ID represents one record? search by name or another parameters can returns one or more records – H. Herzl Jul 04 '19 at 15:18
  • I need function for each parameter. What if I have table with 30 fields? Is there a method to have different function for every field like: GetName(), GetSurname(), GetAge()? Can I relate to function name and not aip/Todo/5 ? – PabloPhones Jul 05 '19 at 15:53