How to Use Routing in ASP.NET Core - Windows ASP.NET Core Hosting 2024 | Review and ComparisonWindows ASP.NET Core Hosting 2024 | Review and Comparison

This article explains how to use ASP.NET Core routing to map actions to corresponding URLs in incoming requests.

Routes specify how actions and URL paths should correspond. It was used to produce URLs that were sent out in replies.

Make a ProductController first, and then, as indicated below, make a Details action.

[HttpGet]
public IActionResult Details(int id)
{
    return View();
}

To create the Details view, right-click on the Details action.

Start by opening your Startup class, and then add the default routing configuration as indicated below.

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
Running your project, then proceed to Product/Details/1

You should observe that the action receives the parameter from the routing if everything goes according to plan.

Proceed similarly and make a new Edit action.

[HttpGet]
public IActionResult Edit(int productId)
{
    return View();
}
The reason you won’t see routing passed to the action is that we have the parameter set to Id rather than productId.

Route attribute asp.net core

You can change your code as demonstrated below by using the route attribute in ASP.NET Core.

[HttpGet("{productId}")]
public IActionResult Edit(int productId)
{
    return View();
}
or using constraints routing.
[Route("Product/Edit/{productId:int}")]
public IActionResult Edit(int productId)
{
    return View();
}
Running your project, then move Product/Edit/2

You can see your parameter from url passed to your action.

Creating a SearchByProduct api as shown below.

[Route("Product/Search/{category}/{keyword}")]
public string[] SearchByProducts(string category, string keyword)
{
    return new[]
    {
         $"{category}, {keyword}"
    };
}
Pressing F5 to run your project, then move Product/Search/furniture/tv

You can also modify your routing as RESTful Routes.

api/product/search/furniture/camera

[Route("api/[controller]")]
public class ProductController : Controller
{
      //... 
}
and modify your action route
[Route("Search/{category}/{keyword}")]
public string[] SearchByProducts(string category, string keyword)
{
    return new[]
    {
        $"{category}, {keyword}"
    };
}
or you can change your configuration to increase the number of default routes.
app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllerRoute(
                    name: "api",
                    pattern: "api/{controller}/{action}/{id?}");
                endpoints.MapRazorPages();
            });
You can specify routes for your application using the MapControllerRoute method, which you can call repeatedly to set different templates for your routes.