Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Pages Default Page in aspnetcore 2

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 setup and thus MVC routing does not apply.

I would think it would be in options somewhere, but I don't see it:

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

I have tried this:

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

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

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Page: /App

Page: /Index

It's possible to resolve this error by removing Pages/Index.cshtml from the project, but I wanted to keep that page as well.

like image 232
Greg Gum Avatar asked Feb 08 '18 11:02

Greg Gum


People also ask

How do I change the start page in .NET Core?

For Asp.Net Core 2.2 right click on Project → Properties → Debug and next to Launch Browser checkbox set path to the startup page you want.

How do I set the default page in aspx?

Right Click on your desired Page and Select Set As Start Page. But what if for Websites that are hosted on IIS. If there is Default. aspx, it would render first.

What is ViewData Razor pages?

ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel.


1 Answers

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
like image 142
EgurnovD Avatar answered Oct 12 '22 16:10

EgurnovD