How to add custom route for razor pages

In Asp.Net MVC, when you needed to add a custom routes for a page, you are doing from your RouteConfig or equivalent. You would add code that may look like below.

                name: "Login",
                url: "login/",
                defaults: new { controller = "Account", action = "Login" });

Aboce mapping will make sure that when somebody types URL like "", the request will go Login action of Account controller. If you don't add above route mapping, then yours of your website will use url as "".

In Razor pages you make sure of @page directive at the top of your page. There you will specify what is route that will users will be using to get to appropriate action. Above example will look like following @page directive in razor page.

@page "/Login"

In my view, when I need to generate link for this login page, I will be using following anchor tag helper.

<a asp-page="/Account/Login" title="Login into your account with QmsBot">&l;i class="zmdi zmdi-key"></i></a>

Above code snippet will generate correct URL to "/login" route for my page. It will not generate URL of "/account/login".




19.8 °C / 67.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags