locked
Appears there is a controller conflict with the Route attribute RRS feed

  • Question

  • User-973886032 posted

    hi guys

    I added  [Route("{Category"}] attribute to my Home Controller, so I can have products like this 

    www.xyz.com/TShirts 

    www.xyz.com/mugs 

    which works fine, and other controllers too work fine, but I noticed that my users controller is hijacked, as I put a breakpoint and noticed www.xyz.com/Users

    goes to the breakpoint on the home page. here is my code

      [Route("/{C}")]
            public async Task<IActionResult> Product(String C)
            {
    
                return View(await _context.Departments_SubCategory.Include(c => c.Departments_Category)
                   .Where(d => d.Departments_Category.Category_Name.Contains(C))
                     .Where(r => r.IsEnabled == true).Select(u => new Departments_SubCategory
                     {
                         CategoryID = u.CategoryID,
                         SubCategory_Name = u.SubCategory_Name,
                         EntryDate = u.EntryDate,
                         Description_Detailed = u.Description_Detailed,
                         Description_Short = u.Description_Short,
                         ProductCount = u.Products.Count()
    
                     }).ToListAsync());

    And the users controller has no code as of yet, as I cant seem to get it hit. But when I removed the route attribute on the HOme controller, it works

       public async Task<IActionResult> Index()
            {
    return View()
    }

    Thursday, August 8, 2019 8:25 AM

All replies

  • User475983607 posted

    I added  [Route("{Category"}] attribute to my Home Controller, so I can have products like this 

    www.xyz.com/TShirts 

    www.xyz.com/mugs 

    which works fine, and other controllers too work fine, but I noticed that my users controller is hijacked, as I put a breakpoint and noticed www.xyz.com/Users

    Correct, www.xyz.com/Users matches the route attribute.  BTW, your route attribute has an syntax error.

    It is difficult to provide assistance without understanding your routing requirements,  As shown above the approach does not make sense.

    Thursday, August 8, 2019 10:38 AM
  • User-973886032 posted

    Correct, www.xyz.com/Users matches the route attribute.  BTW, your route attribute has an syntax error.

    It is difficult to provide assistance without understanding your routing requirements,  As shown above the approach does not make sense.

    I found out I was using the wrong route attribute, as I should not have used /

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2

    Attribute routing
    Attribute routing uses a set of attributes to map actions directly to route templates. In the following example, app.UseMvc(); is used in the Configure method and no route is passed. The HomeController will match a set of URLs similar to what the default route {controller=Home}/{action=Index}/{id?} would match:
    
    C#
    
    Copy
    public class HomeController : Controller
    {
       [Route("")]
       [Route("Home")]
       [Route("Home/Index")]
       public IActionResult Index()
       {
          return View();
       }
       [Route("Home/About")]
       public IActionResult About()
       {
          return View();
       }
       [Route("Home/Contact")]
       public IActionResult Contact()
       {
          return View();
       }
    }

    Friday, August 9, 2019 5:32 AM