.NET MVC Area Routing Root Parameters

382 Views Asked by At

I have an area called Suppliers, with a controller called supplier.
I want to route urls such as ~/{SupplierName}. ie root/supplier1 I want to handle these requests in my supplier controller index action.

How do I set up the route config for this?

1

There are 1 best solutions below

0
On

ASP.NET 5, MVC 6

You could try an action route....

[Route("/{SupplierName}")]
public async Task<IActionResult> Index(string SupplierName)
{
  // do stuff
  return View();
}

I would recommend /suppliers/{SupplierName} to avoid any conflicts.

OR via Startup.cs (just adapt the below):

 // Map routes
            app.UseMvc(routes =>
            {
                // The first parameter will be examined to see if it matches any areas
                routes.MapRoute(name: "areaRoute",
                  template: "{area:exists}/{controller}/{action}/{id?}",
                  defaults: new { controller = "Login", action = "Index" });

                // Then we will default to areas that do exist
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

Hope this helps. Dan.