'Custom Home/Index route in ASP.NET Core 6 MVC

I'm trying to create a custom route for the home controller on the index action with a required property ie. localhost/{something}

Would think adding something like this would work

app.MapControllerRoute(
    name: "HomeUrlKey",
    pattern: "{controller=Home}/{action=Index}/{urlKey}");

before the default route, with the home controller having

[Route("HomeUrlKey")]
public IActionResult Index(string urlKey)
{
    return View("HomeLocation");
}

I've also created a view called HomeLocation.cshtml - but no luck, any suggestions?



Solution 1:[1]

Figured it out, the controller needed to be

 [Route("/{urlKey:alpha}")]
    public IActionResult Index(string urlKey)...

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0#route-constraints

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1