1

How can i make my user routes in mvc with core 2? Early, in just ASP.NET i used:

context.MapRoute(
    "index",
    "index",
    defaults: new
    {
        area = "AnoterArea",
        controller = "Base",
        action = "Index"
    });

But now what should i do? Im trying to do something like..

app.UseMvc(routes =>
{
    routes.MapAreaRoute(
        "index",
        "Index",
        "{controller}/{action}/{id?}",
        new 
        {
            controller ="Base",
            action = "Index"
        });
});

What do you say?

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
msmrc
  • 145
  • 3
  • 11

1 Answers1

0

In ASP MVC Core, you typically add those routes to the Startup.cs file. Inside the Configure() method, you should see something resembling this code:

app.UseMvc(routes =>
{
   routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");   
});

From there, you can add additional routes like this above the default route:

app.UseMvc(routes =>
{
         //additional route
         routes.MapRoute(
            //route parameters    
        );

        //default route
        routes.MapRoute(
          name: "default",
          template: "{controller=Home}/{action=Index}/{id?}"
         );   
});

In your case, you would want to add something like the following:

routes.MapRoute(
  name: "index",
  template: "{area:exists}/{controller=Base}/{action=Index}/{id?}"
 );  

Additionally, you will need to make sure that your Controller is within the Areas folder and the Controller action is decorated with the Area name:

[Area("AnotherArea")]
public ActionResult Index()
{
    return View();
}

Here is a link to a nice article about routing in .NET Core:

https://exceptionnotfound.net/asp-net-core-demystified-routing-in-mvc/