0

I'm new to ASP.NET Core Web API and trying to implement a custom route. Here is my controller:

using ...

namespace FoodDeliveryServer.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MenusController : ControllerBase
{
    private readonly FoodDeliveryContext _context;

    public MenusController(FoodDeliveryContext context)
    {
        _context = context;
    }

    // GET: api/Menus
    [HttpGet]
    public IEnumerable<Menu> GetMenu_1()
    {
        return _context.Menu;
    }

    // rest of the methods

    // POST: api/Menus
    [HttpPost]
    public async Task<IActionResult> PostMenu([FromBody] Menu menu)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _context.Menu.Add(menu);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetMenu", new { id = menu.Id }, menu);
    }

    // POST: api/Menus/filter
    [HttpPost("filter", Name = "Filtermenu")]
    public async Task<IActionResult> FilterMenu([FromBody] bool isActive)
    {
        return Ok(_context.Menu.Where(m => m.IsActive == isActive));
    }

    private bool MenuExists(long id)
    {
        return _context.Menu.Any(e => e.Id == id);
    }
}
}

Now, I'm trying to hit the filter route from POSTMAN and getting 404. Other standard routes are working fine.

POST
http://localhost:5000/api/Menus/filter

Body -> raw -> JSON

{
    "isActive": true
}

Headers -> Accept -> application/json
Headers -> Content-Type -> application/json

What am I doing wrong here?

I looked at this link also but still having problems:

ASP.Net Core Web API custom route not working

2
  • Have you done a full rebuild? Sometimes build doesn't seem to refresh everything. Commented Apr 11, 2020 at 8:26
  • Yes I did :( still not working Commented Apr 11, 2020 at 8:28

2 Answers 2

1

You can use the [Route("")] decorator on your actions too. Otherwise it will create routes upon what you defined in StartUp.Configure.

// POST: api/Menus/filter
[Route("filter")]
[HttpPost("filter", Name = "Filtermenu")]
public async Task<IActionResult> FilterMenu([FromBody] bool isActive)
{
    return Ok(_context.Menu.Where(m => m.IsActive == isActive));
}

Should work for you. The '/api/Menus/' part will be inherited from your [Route("api/[controller]")] definition on controller level

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

Comments

0

I resolved the issue with the help of @Marius. Here is what I did:

    // POST: api/Menus/filter
    [HttpPost]
    [Route("filter", Name = "Filtermenu")]
    public async Task<IActionResult> FilterMenu([FromBody] Menu menu)
    {
        return Ok(_context.Menu.Where(m => m.IsActive == menu.IsActive));
    }

Looks like, we need to provide a class to read FromBody

Comments

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.