Creating a route using convention routing
In this recipe, we will learn how to create a route using convention routing.
How to do it...
We can do it in two ways. The first way to create a route using convention routing is as follows:
- We add
services.AddMvc()
toConfigureServices
:
app.AddMvc();
- 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:
- We add
services.AddMvc()
toConfigureServices
:
app.AddMvc();
- Now, it's time to add
app.UseMvc()
method call insideConfigure()
method:
app.UseMvc();
- We add attribute routing to the controller and/or action:
[Route("api/[controller]")] public class ValuesController : Controller
How it works...
We can mix the first way with the second way by adding routes to routescollection
in the configure
...