I'm using ASP.Net Core 6.0 with Razor pages. I set each page's title with ViewData["Title"] = "Home" in the the .schtml files. I also set the metadata description with <meta name="description" content="Home page." />.
I would like to display a list of the current pages on my home page. This list would include the page title and meta description along with the Url so I can link to the page. I can get the Urls from the EndpointDataSource.
I could add the information into a database and update it every time I add a new page. I don't like the idea of creating two places for the same data without some way to keep them in sync.
Is there a way to get this information from the existing pages?
I've tried putting the page title in a base class that inherits from PageModel. I was not able to access the PageModel of another page from C#. I tried using the page Urls to load the pages. I don't know how to do that either.
I searched for a solution and I didn't find any. I could ask an AI bot but I don't know what to ask.



Seems you are using Razor page. If you investigate you would seen
endpointDataSource.Endpointsis type ofEndpointDataSourcewhich contains list ofEndpointswhere we can get information related to endpoint/page or route metadata.As you can see
Endpointsis a type ofIReadOnlyListand it's parent classEndpointDataSourceis aabstructclass so yes we cannot directly instantiate it but of course we can call it anywhere. Due to itsabstruction levelwe need to inroduce constructor to invoke it.Yes, we do have the better in fact, eligant way to invoke it to get the all page name therefore, its meta-data as well. Here is the complete demo for you.
Asp.net core Razor Page For Getting All Containing Page Name:
Note: I have injected
EndpointDataSourceusingListAllRazorPagesModelconstructor to invokeEndpointsover it.View:
Output:
Note: If you would like to know more details on EndpointDataSource you could check our official document here.