Chris Rice
Chris Rice

Reputation: 819

Razor Pages not finding subfolder index page

This may be a stupid question but i can't seem to get a Razor Pages project to locate index files placed in subfolders properly.

Based on this introduction to razor pages: https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio

The following structure is valid.

File name and path                  matching URL
/Pages/Index.cshtml                 / or /Index
/Pages/Contact.cshtml               /Contact
/Pages/Store/Contact.cshtml         /Store/Contact
/Pages/Store/Index.cshtml           /Store or /Store/Index

The last one listed there is what i'm having problems with. It seems that if i try and specify /Store it will not manage to locate the right page. If i specify /Store/Index it will work though..

Here's how to replicate this.

  1. Create ASP.NET Core WebApp project with RazorPages
  2. Create a folder under pages called Store
  3. Create a razor page under that folder called Index
  4. Update the _Layout page to point to asp-page="/Store"
  5. Try and run the app and click on the navigation link and see it won't load the page
  6. Update the link to asp-page="/Store/Index" and run the app to see that the link is properly routed now

Am i completely missing something here?

Upvotes: 1

Views: 2177

Answers (1)

Mike Brind
Mike Brind

Reputation: 30110

The asp-page attribute requires the name of the page. It is not a URL. Since the page is named Index, you need to include that. The "name" of the page is basically its relative path rooted in Pages, minus the file extension. But the important part to note is that you must include "Index" when trying to link to a file named Index.

Upvotes: 2

Related Questions