Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Default Page in ASP.NET Core Razor Page to Login Page

I am changing Default Page from /Home/Index to /Identity/Account/Login using ASP.NET Core Razor Page but it is always loading /Home page as default page.

services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddRazorPagesOptions(options =>
                {
                    options.Conventions.Clear();
                    options.AllowAreas = true;
                    options.Conventions.AddAreaPageRoute("Identity", "/Login", "/Identity/Account/Login");
                });

It should load the Login Page Model as default page. Please help me to solve above problem. Waiting for your quick response.

like image 491
GKB Avatar asked Oct 28 '25 19:10

GKB


1 Answers

To map your site's root to the /Identity/Account/Login page in ASP.Net Core (v2.1 and above) you can follow these instructions:

  1. Undo the changes you've made in the ConfigureServices() method. (I.e. .AddRazorPagesOptions(...))

  2. Change the @page declaration at the top of the Login.cshtml template file to @page "/"

  3. If you haven't already, delete the Index.cshtml and Index.cs page files.

Step 1 is for cleaning up.

Step 2 is to configure the Login to the root path /. (more info here)

Step 3 is necessary because if the Index page files are not deleted, they will be mapped by convention to the root path and will conflict with the changes in step 2. This will throw a AmbiguousMatchException when you browse to the root path.

like image 61
urig Avatar answered Oct 31 '25 09:10

urig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!