JQuery Mobile
JQuery Mobile

Reputation: 6301

Migrating ASP.NET MVC Routes to ASP.NET vNext

I have an ASP.NET MVC app. I am learning ASP.NET vNext. To do that, I decided to port my existing app over to vNext. The thing I'm not sure about is, how to port over my routes.

In my origin ASP.NET MVC app, I have the following:

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.RouteExistingFiles = true;

  routes.MapRoute(
    name: "Index",
    url: "",
    defaults: new { controller = "Root", action = "Index" }
  );

  routes.MapRoute(
    name: "Items",
    url: "items/{resource}",
    defaults: new { controller = "Root", action = "Items", resource = UrlParameter.Optional }
  );

  routes.MapRoute(
    name: "BitcoinIntegration",
    url: "items/available/today/{location}",
    defaults: new { controller="Root", action="Availability", location=UrlParameter.Optional }
  );

  routes.MapRoute(
    name: "BlogPost1",
    url: "about/blog/the-title",
    defaults: new { controller = "Root", action = "BlogPost1" }
  );
}

Now in this ASP.NET vNext world, I'm not sure how to setup routes. I have the following:

Startup.cs

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;

namespace MyProject.Web
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseErrorPage();

            app.UseServices(services =>
            {
                services.AddMvc();
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
            });

            app.UseMvc();
            app.UseWelcomePage();
        }
    }
}

Still, I'm not sure of two things:

  1. How to add the routes I defined in RouteConfig.cs previously.
  2. How to use views/home/Index.cshtml as my default path in place of app.UseWelcomePage().

Upvotes: 6

Views: 4134

Answers (1)

Mrchief
Mrchief

Reputation: 76238

Registering routes: There are few changes but the overall approach remains same. Here's your refactored RouteConfig:

RouteConfig.cs

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;

public class RouteConfig 
{
  // instead of RouteCollection, use IRouteBuilder
  public static void RegisterRoutes(IRouteBuilder routes)
  {
    // routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); -> gone
    // routes.RouteExistingFiles = true; -> gone

    routes.MapRoute(
      name: "Index", 
      template: "", 
      defaults: new { controller = "Root", action = "Index" }
    );

    // instead of UrlParameter.Optional, you use the ? in the template to indicate an optional parameter
    routes.MapRoute(
      name: "Items", 
      template: "items/{resource?}", 
      defaults: new { controller = "Root", action = "Items" }
    );

    ... // ignored for brevity (since their registration is along the same lines as the above two).
  }
}

Startup.cs

public void Configure(IApplicationBuilder app)
{
  // ... other startup code
  
  app.UseMvc(RouteConfig.RegisterRoutes);  
  
  // ... other startup code
}

Note: You can very well inline the route registration here, but I prefer having it in separate file to de-clutter Startup.cs

To point UseWelcomePage to one of your own, look at the different overloads here.

Disclaimer: Since vNext is still in beta is undergoing churn every hour, the code I show here could quickly become outdated, even in the next minute or the next hour!

Upvotes: 4

Related Questions