Routing through the ages

In earlier times, when ASP.NET was just Web Forms, URL routing was strictly bound to physical files. In order to implement viable URL convention patterns, the developers were forced to install/configure a dedicated URL rewriting tool using either an external ISAPI filter such as Helicontech's SAPI Rewrite or, starting with IIS7, the IIS URL Rewrite Module.

When ASP.NET MVC was released, the routing pattern was completely rewritten, and developers could set up their own convention-based routes in a dedicated file (RouteConfig.cs or Global.asax, depending on template) using the Routes.MapRoute method. If you've played along with MVC 1 through 5 or WebAPI 1 and/or 2, snippets like this should be quite familiar to you:

    Routes.MapRoute( 
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);

This method of defining routes, strictly based on pattern matching techniques used to relate any given URL requests to a specific Controller's Actions, went by the name of Convention-based routing.

ASP.NET MVC5 brought something new, as it was the first version supporting the so-called Attribute-based routing. This approach was designed as an effort to give a more versatile approach to developers. If you used it at least once, you'll probably agree that it was a great addition to the framework, as it allowed developers to define routes within the Controller file. Even those who chose to keep the convention-based approach could find it useful for one-time overrides, such as the following, without having to sort it out using some regular expressions:

 [RoutePrefix("v2Products")] 
public class ProductsController : Controller
{
[Route("v2Index")]
public ActionResult Index()
{
return View();
}
}

In ASP.NET Core MVC (aka MVC 6), with the routing pipeline being completely rewritten, attribute-based routing is quickly becoming a de-facto standard, replacing the convention-based approach in most boilerplates and code samples. However, setting routing conventions using the Routes.MapRoute() method is still a viable way to define a limited amount of high-level, default routing rules, as our Startup.cs file clearly shows (relevant lines are highlighted):

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

This code snippet, taken from the Configure method, demonstrates that convention-based routing is still a viable approach when developing an SPA. The only difference between ASP.NET 4.x and earlier releases is the fact that the routes are directly sent to the MVC middleware when we add it to the HTTP request pipeline as part of its configuration parameters, thus resulting in a more streamlined approach.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43