Razor Pages default page in aspnetcore 2

By default, the Razor Page application goes to Home / Index

Is there any way to change this on the Home / App?

This is fairly simple in MVC, but Razor pages using a different routing setting and thus MVC routing are not applied.

I would think that it will be somewhere in the settings, but I do not see this:

services.AddMvc()
            .AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizeFolder("/Account/Manage");
                options.Conventions.AuthorizePage("/Account/Logout");
                options. ??SetDefaultPage??
            });

I tried this:

options.Conventions.AddPageRoute("/App", "");

But now two default routes are found and an error is generated:

AmbiguousActionException: several actions are performed. The following actions were consistent with route data and met all restrictions:

Page: / App

Page: / Index

You can resolve this error by deleting Pages / Index.cshtml from the project, but I also wanted to save this page.

+9
source share
5

, . , Index . , , IndexFileName - static PageRouteModelFactory class :

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

, RazorPagesOptions. ASP.NET , GitHub .

+3

Pages/Index.cshtml . :

  1. options.Conventions.AddPageRoute("/App", "");
  2. Pages/Index.cshtml
+3

, Microsoft.AspNetCore.Rewrite:

, , :

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

- https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?tabs=aspnetcore2x

+1

, Visual Studio, // .

0

- Index OnGet, , :

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

, OnGet.

0

Source: https://habr.com/ru/post/1693361/


All Articles