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.

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

Aboce mapping will make sure that when somebody types URL like "http://www.mysite.com/Login", 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 "http://www.mysite.com/account/login".

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".

comments powered by Disqus

Blog Tags