Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().
like image 881
JQuery Mobile Avatar asked Jan 06 '15 13:01

JQuery Mobile


People also ask

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What is custom route in MVC?

When you request any page into MVC Application, it will go through the Routing Architecture. Your Routing system will decide your URL pattern. The default routing algorithm is like {controller}/ {action}/ {id} patterns. But it can be possible to change that pattern using Custom Routes.

What is ASP Net mvc6?

ASP.NET MVC is a web application framework developed by Microsoft that implements the model–view–controller (MVC) pattern. It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary.


1 Answers

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! If that happens, before you down vote for "this is not working", drop me a comment and I'll try my best to bring it up to the current level.

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.

like image 51
Mrchief Avatar answered Sep 20 '22 09:09

Mrchief