Skip to main content

📓 Custom URL Paths

In the last lesson, we learned how we can configure routes like this one: localhost:5000/home/goodbye.

However, this URL isn't very user-friendly. Users shouldn't have to type in the name of a controller. Thankfully, we can use a route decorator to easily declare custom URL paths for each route.

Custom URLs with Route Decorators


Let's revisit our HomeController. With a single line of code, we can override which URL path matches which route.

FriendLetter/Controllers/HomeController.cs
using Microsoft.AspNetCore.Mvc;

namespace FriendLetter.Controllers
{
public class HomeController : Controller
{

[Route("/hello")]
public string Hello() { return "Hello friend!"; }

[Route("/goodbye")]
public string Goodbye() { return "Goodbye friend."; }

[Route("/")]
public string Letter() { return "Our virtual postcard will go here soon!"; }

}
}

The route decorator [Route("/hello")] provides additional information to a route we define. In our case, the decorator is overriding the default URL path that corresponds to each of the two routes:

  • Instead of needing to visit /home/hello to activate our Hello() route, we now only have to visit /hello.

  • Similarly, instead of visiting /home/goodbye to invoke Goodbye(), the URL localhost:5000/goodbye will now invoke this route.

Root Path

We also added a third new route for our virtual postcard. This will route to "/". In other words, we don't need to append a path at all. If we navigate to localhost:5000, this route will be activated. This is called a root path and it's the home page for our site. All sites should have a home page, which means all your projects should have a root route.