Choose path index

Asked

Viewed 70 times

1

Hello, I’m with a project . NET Core Razor.

My page index is inside the folder Pages, and I would like to put my page index inside the Test folder that is inside the pages folder, for example Pages/Teste.

Does anyone there know how I can do it?

And if I want to change the name of that folder Pages, how do I make it work properly?

STRUCTURE:

inserir a descrição da imagem aqui

  • 1

    Samuel, post the class code Startup

2 answers

0

This setting by default (i.e., the RootDirectory is configured for the folder /Pages) and configures your pages are inside the folder /Pages and follows a convention where the subfolders have the name of the route to load the pages. If you change the index for the way Pages/Teste in the browser should also be named because it follows this directory naming convention, example:

http://localhost/teste/index

for this page to be loaded. If you want to create a route in addition then do as in the example below:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()                
        .AddRazorPagesOptions(options =>
        {           
            // faz a rota para a página padrão
            options.Conventions.AddPageRoute("/Teste/index", "");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

and in the browser:

http://localhost      

following the same as the standard configuration, but otherwise arranged.


And if I want to change the name of this Pages folder, how do I work properly?

If you change the folder name you also need to configure the default settings as follows:

public void ConfigureServices(IServiceCollection services)
{   
    services.AddMvc()                
        .AddRazorPagesOptions(options =>
        {
            //configurando o nome da pasta root padrão
            options.RootDirectory = "/Pages1";
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

where options.RootDirectory enables you to configure the default folder name of your pages.

All two changes without a context and perhaps even without need will not bring any benefit, try working with the standard mode that will simplify your development environment.

References

  • 1

    Great explanation. Thank you for the answer.

0


You need to specify the page in the Razor view engine that is in the Startup.Cs file, configure the Configuraeservices method this way and it should work:

 services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("", "Pages/Teste");
});
  • 1

    Thanks for the answer. This "Test" would be the page, right? So in case it should be, for example, Test.cshtml?

  • 1

    already managed, worked perfectly, thank you

  • 1

    Show my friend, happy to help you =D 0/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.