Razor Pages Default Page in aspnetcore 2

前端 未结 5 509
感动是毒
感动是毒 2021-01-11 19:51

By default a Razor Page app goes to Home/Index

Is there a way to change this to Home/App?

This is quite easy in MVC, but Razor pages using a different routing

相关标签:
5条回答
  • 2021-01-11 20:24

    In my case the ambiguity was caused by Pages/Index.cshtml left in project. This worked:

    1. options.Conventions.AddPageRoute("/App", "");
    2. remove or rename Pages/Index.cshtml
    0 讨论(0)
  • 2021-01-11 20:26

    If all you need this for is to debug in Visual Studio, you can just set the start page under Project Properties/Debug/Launch Browser.

    0 讨论(0)
  • 2021-01-11 20:29

    Another way is to simply redirect from the Index OnGet method, like so:

        public class IndexModel : PageModel
        {
            public IActionResult OnGet()
            {
                return Redirect("/Welcome");
            }
        }
    

    Notice that I have change the return type of the OnGet method.

    0 讨论(0)
  • 2021-01-11 20:30

    I solved the issue by using Microsoft.AspNetCore.Rewrite:

    Then adding code to replace the default Index action, in my case with Portfolio:

    var options = new RewriteOptions()
        .AddRedirect("^", "portfolio"); // Replace default index page with portfolio
    

    More detailed article about rewrite options - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?tabs=aspnetcore2x

    0 讨论(0)
  • 2021-01-11 20:40

    Pretty sure it isn't possible. The docs say the runtime controls the search for Index as the default. I couldn't find where that happens in the current release, but IndexFileName is a static in the new internal PageRouteModelFactory class added to the upcoming release:

    private static readonly string IndexFileName = "Index" + RazorViewEngine.ViewExtension;

    It doesn't seem like it would be difficult to just add a config property to RazorPagesOptions, though. The ASP.NET guys are pretty responsive, I'd open a request as a GitHub issue and hope for the best.

    0 讨论(0)
提交回复
热议问题