简体   繁体   中英

Retrieve a list of all razor pages to a globally accessible variable

I am eventually trying to create a nav menu that can auto matically populate itself with the sites pages. I need to get a list of all the endpoints and store them to a database to be accessed by the logic (this will just be.txt or.json for now).

In my original post here: https://stackoverflow.com/questions/74988601/how-can-i-can-get-a-list-of-razor-pages-in-a-razor-pages-app I was able to get a list of all endpoints with a constructor, but unable to access these variables from anywhere but that specific razor page view. Thanks to Md Farid Uddin Kiron .

I tried simply copying the list to a variable in another class ("endpointStringTest" in the "JSONTest" class):

public class IndexModel : PageModel
{
    public readonly IEnumerable<EndpointDataSource> _endpointSources;
    public IndexModel(IEnumerable<EndpointDataSource> endpointDataSources)
    {
        _endpointSources = endpointDataSources;
    }

    public IEnumerable<RouteEndpoint> EndpointSources { get; set; }
    public void OnGet()
    {

        EndpointSources = _endpointSources
                    .SelectMany(es => es.Endpoints)
                    .OfType<RouteEndpoint>();

        foreach(var endpointSource in EndpointSources) 
        { 
            Console.WriteLine(endpointSource);
            Debug.WriteLine(endpointSource.ToString());
            JSONTest.endpointStringTest.Add(endpointSource.DisplayName);
        }
        Console.WriteLine(JSONTest.endpointStringTest);
        Debug.WriteLine(JSONTest.endpointStringTest);
    }
}

But this results in a null reference. If i understand correctly, this is due to constructors being initialized and deleted before normal classes are initialized? is there a way to work around this?

I also tried turning the above constructor into a regular method, but the variables were always null. I don't fully understand where "endpointDataSources" is getting it's value. It's obviously something to do with being initialized within a constructor, as thats the only time it's not null.

By writing the endpoints to a text file (or theoretically any kind of database) i can simply pass the info via that text file.

Index model: Gets a list of all page paths and then writes them to a text document. Obviously in practice you would want to format this as.json, .xml or to a database etc..

    public class IndexModel : PageModel
{
    public readonly IEnumerable<EndpointDataSource> _endpointSources;
    public IndexModel(IEnumerable<EndpointDataSource> endpointDataSources)
    {
        _endpointSources = endpointDataSources;
    }

    public IEnumerable<RouteEndpoint> EndpointSources { get; set; }
    public void OnGet()
    {

        EndpointSources = _endpointSources
                    .SelectMany(es => es.Endpoints)
                    .OfType<RouteEndpoint>();

        //string filePath = App.enviroment;
        StreamWriter writer = new StreamWriter("pagesTest.txt");
        foreach (var endpointSource in EndpointSources) 
        { 
            Console.WriteLine(endpointSource);
            Debug.WriteLine(endpointSource.ToString());

            
            writer.WriteLine(endpointSource.ToString());
            
        }
        writer.Close();
    }
}

PageListGetter model: this copies the contents of the previously created document and stores them in a variable.

    public class PageListsModel : PageModel
{
    public string? pageList;
    public void OnGet()
    {
        StreamReader reader = new StreamReader("pagesTest.txt");
        pageList = reader.ReadToEnd();
        reader.Close();
    }
}

I have also tested this on an Azure published version of the site and it works fine. I was concerned the filepaths may not line up or may be inaccessible from a regular model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM