...

/

Filter to Define a Custom Route and to Cache a Response

Filter to Define a Custom Route and to Cache a Response

Learn about defining a custom route in ASP.NET Core MVC and filter to cache a response.

Let's dive into using the filter for defining a custom route and caching a response.

Using a filter to define a custom route

We might want to define a simplified route for an action method instead of using the default route. For example, to show the privacy page currently requires the following URL path, which specifies both the controller and action: https://localhost:5001/home/privacy

We could make the route simpler, as shown in the following link: https://localhost:5001/private

Let’s see how to do that:

Step 1: In HomeController.cs, add an attribute to the Privacy method to define a simplified route, as shown in the following code:

Press + to interact
[Route("private")]
[Authorize(Roles = "Administrators")]
public IActionResult Privacy()

Step 2: Start the website. ...