How to do it...

We can do it in two ways. The first way to create a route using convention routing is as follows:

  1. We add services.AddMvc() to ConfigureServices:
app.AddMvc(); 
  1. We add app.UseMvc() to configure the route definitions:
app.UseMvc(routes => 
                { 
                routes.MapRoute( 
                    name: "default", 
                    template: "{controller=Home}/{action=Index}/{id?}"); 
});

The second way to create a route using convention routing is as follows:

  1. We add services.AddMvc() to ConfigureServices:
app.AddMvc(); 
  1. Now, it's time to add app.UseMvc() method call inside Configure() method:
app.UseMvc(); 
  1. We add attribute routing to the controller and/or action:
[Route("api/[controller]")] public class ValuesController : Controller

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.