How to make Login page as a default route in ASP .NET Core 2.1?

*爱你&永不变心* 提交于 2019-12-23 09:29:30

问题


I am beginner in ASP .NET Core 2.1 and working on project which is using ASP .NET Core 2.1 with individual authentication. I want to make my login page as my default route instead of Home/Index:

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

Any help how can i change it as ASP .NET Core 2.1 as Login is now used as a razor page instead of MVC Action View.


回答1:


Use this in ConfigureServices method.

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

then in Configure method

 app.UseMvc(routes =>
        {
            routes.MapRoute(
               name: "default",
               template: "{controller=Home}/{action=Index}/{id?}");

        });



回答2:


I solve this by using this code in ConfigureServices function (Startup.cs)

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



回答3:


this may help, i haven't had a need to change default page myself

https://exceptionnotfound.net/setting-a-custom-default-page-in-asp-net-core-razor-pages/




回答4:


Just use this in your configuration. This will add AuthorizeAttribute to your page

services.AddMvc()
.AddRazorPagesOptions(options =>
{
    options.Conventions.AuthorizePage("/Home/Index");
});

Or change the Default route like this :

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Employees/Index", "");
});

See this page if necessary : https://docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.1




回答5:


Insert this code to ConfigureServices() in Startup.cs

{
   services.AddMvc().AddRazorPagesOptions(options =>
   {
       //Registering 'Page','route-name'
       options.Conventions.AddPageRoute("/Account/Login", "");
   });
}
  • Remember to remove any route name on "/Account/Login" Action declaration



回答6:


After along time I solved it. Need add ALLOW for AREAS =>

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorPagesOptions(options =>
        {
            options.AllowAreas = true; //--working after add this line
            options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
        });


来源:https://stackoverflow.com/questions/51506419/how-to-make-login-page-as-a-default-route-in-asp-net-core-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!