1
votes

I want to create routes for rendering views (web routes) and routes for administration purpose (api routes). So I created a directory structure like this:

 controllers //root controller folder
    ProductController
    Api //subfolder of controllers folder
       Productcontroller

I have classes with the same name and it is messing up my routes. The "api routes" are overlapping the "web routes".

This is how I defined my api routes

namespace Loja.Controllers.Api

[Produces("application/json")]
[Route("api/Produtos")]
public class ProdutosController : Controller
{
     // GET: api/<controller>
     [HttpGet]
     public IEnumerable<string> Get()
     {
        return new string[] { "value1", "value2" };
     }
}

This is how I defined my web routes :

namespace Loja.Controllers
{
    public class ProdutosController : Controller
    {
        [Route("/produtos/{slug}")]
        public IActionResult Get(string slug) 

When I try to generate a link to my web route I am directed to the api route. Why?

<a asp-controller="Produtos" asp-action="Get"  asp-route-slug="@produto.Slug">

When I click the link I am directed to

http://localhost:5000/api/produtos?slug=assadeira-grande-40-x-27-cm-109dcc
2

2 Answers

0
votes

One solution is to use areas and create a hyperlink like below:

<a asp-area="Public" asp-controller="Home" asp-action="About">Test</a>

enter image description here

Or you could manually create the a href;

<a href="/Produtos/Get/@produto.Slug">Link</a>
0
votes

Change the [Route("/produtos/{slug}")] in your Web controller like below

namespace Loja.Controllers
{
public class ProdutosController : Controller
{
    [Route("/produtos/Get/{slug}")]
    public IActionResult Get(string slug)