ASP.NET Core: Routing in ASP.NET Core

By FoxLearn 3/16/2020 7:13:51 PM   2.81K
This post shows you How to use routing in ASP.NET Core to match the URLs of incoming requests and map to actions.

Routes describe how URL paths should be matched to actions. It used to generate URLs sent out in responses.

Creating a ProductController, then create a Details action as shown below.

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

Right-clicking on Details action, then create Details view.

Opening your Startup class, then add a configuration your default routing as shown below.

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

Running your project, then move to Product/Details/1

If everything works well you will see that the parameter from routing is passed to the action.

Do the same way, create a new Edit action

[HttpGet]
public IActionResult Edit(int productId)
{
    return View();
}

You will not see routing passed to the action, because we configured the parameter to be Id instead of productId

Route attribute asp.net core

You can use route attribute asp.net core to modify your code as shown below.

[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 also expand your default routes by modifying your configuration.

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllerRoute(
                    name: "api",
                    pattern: "api/{controller}/{action}/{id?}");
                endpoints.MapRazorPages();
            });

The MapControllerRoute method allows you to define routes for your application, and you can call it multiple times to set multiple your route template.