DotNetAnalyzers / AspNetCoreAnalyzers

MIT License
62 stars 4 forks source link

More than one method with the same route #8

Open JohanLarsson opened 5 years ago

JohanLarsson commented 5 years ago
namespace Meh
{
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;

    [ApiController]
    public class OrdersController : Controller
    {
        private readonly Db db;

        public OrdersController(Db db)
        {
            this.db = db;
        }

        [HttpGet("api/orders/{id}")]
        public async Task<IActionResult> GetOrder([FromRoute]int id)
        {
            var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
            if (match == null)
            {
                return this.NotFound();
            }

            return this.Ok(match);
        }

        [HttpGet("api/orders/{id}")]
        public async Task<IActionResult> GetOrderById([FromRoute]int id)
        {
            var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
            if (match == null)
            {
                return this.NotFound();
            }

            return this.Ok(match);
        }
    }
}
NinjaKitt3n commented 5 years ago

Need to ensure you only trigger on duplicate verbs at the same route - since it's fine to both GET and POST to the same endpoint to obtain a collection or create a new entity.