How to change starting page, using Razor Pages in .NET Core 2?

假装没事ソ 提交于 2019-12-08 17:30:28

问题


I wanted to set my starting page to /Members/Index.

When I was using MVC, I configured it as following:

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

But now I'm trying new Razor Pages approach, and now Members is folder and Index is Razor Page. How to set this page in folder as starting one?

I can add Index page in root directory and make redirect there, but I was looking for something cleaner.


回答1:


For testing purposes you can change the start page by going to the Properties window for the web project and select the Debug tab. On the 'Launch browser' line enter the starting path




回答2:


Add the following to your ConfigurationServices function in Startup.cs

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

If you have another index page you'll probably need to delete or rename it.




回答3:


The simplest way to designate a "starting" page beginning with ASP.NET Core 2.1 is using the @page directive in the cshtml file. This feature is explained in this blog post and the directive official documentation is this section on Razor Page syntax.

For example, designating the route as "/" causes this page to become the default page:

@page "/"
@model sample.Pages.startModel
@{
    ViewData["Title"] = "Start Page";
}

<h2>Start Here!</h2>
...



回答4:


I am using this way

services.AddMvc()
    .AddRazorPagesOptions(options => 
    {
        options.AllowAreas = true;
        options.Conventions.AddAreaPageRoute("Home", "/Index", "");
    });

My folder structure is:

 - Area
   |_ Home
      |_ Pages
         |_ Index.cshtml
   |_ //other areas



回答5:


Try following code at AppStart->RouteConfig.cs :

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Members", action = "Index"}
        );


来源:https://stackoverflow.com/questions/46117717/how-to-change-starting-page-using-razor-pages-in-net-core-2

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