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:
views/home/Index.cshtml
as my default path in place of app.UseWelcomePage()
.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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With