Kemal Tezer Dilsiz Kemal Tezer Dilsiz - 3 months ago 16
C# Question

How to migrate routes from .NET to ASP.NET Core? (Specific example)

I am working on a migration from .NET and .NET Core and I am having difficulties with the routing configuration in startup.cs.

From the ASP.NET project, we had the following routing code:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}


Now, I am trying to find something that would simply replace this functionality. What I have been working on so far is the following:

app.UseMvc(routes =>
{
routes.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "GHS", action = "RequestForm" });
});


this works for a single controller and action but I am looking for something that would "RegisterAllAreas".

I have found the following article from Microsoft but was unable to find anything that specifically matched this. https://docs.asp.net/en/latest/fundamentals/routing.html

Is there a way to map all the routes similar to how it was done in .NET code?

Extra information:

I think in the previous version, the url was generated from the View somehow because only the following urls work:

http://localhost:23046/contact-us.html

http://localhost:23046/ghs-request.html

Update:

I have just realized that my project has two files named "contact-us.html" and "ghs-request.html" and these html files has the connection to the javascript files. Therefore, I only need to connect my View forms to these html scripts.

Answer

Try this

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

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