Changing the Default Page in ASP.NET Core Razor Pages

Shervan360 1,481 Reputation points
2023-07-21T15:08:51.8133333+00:00

Hello,

After I created some Razor Pages in our app, I changed Root directory from Pages to My.

Now how can I change Model names from Pages to My?

It seems the app works fine with Pages. Should I change Model names from Pages to My?

Screenshot 2023-07-21 075853

namespace WebApplication6
{
     public class Program
     {
          public static void Main(string[] args)
          {
               var builder = WebApplication.CreateBuilder(args);
               builder.Services
                    .AddRazorPages()
                    .AddRazorPagesOptions(opt => opt.RootDirectory = "/My");

               var app = builder.Build();

               app.UseHttpsRedirection();
               app.UseStaticFiles();
               app.UseRouting();
               app.UseAuthorization();
               app.MapRazorPages();
               //app.MapGet("/", () => "Hello World!");

               app.Run();
          }
     }
}

Screenshot 2023-07-21 075835


ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,022 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 52,826 Reputation points
    2023-07-21T15:57:48.85+00:00

    Namespaces can be anything you want. The default when VS creates a class file is the project plus the folder names. But you do not have to follow.

    if you have been creating models in the pages folder, but rename to my, VS will not rename existing classes. New class files will get the new default.


  2. Zhi Lv - MSFT 29,946 Reputation points Microsoft Vendor
    2023-07-24T07:43:10.9666667+00:00

    Hi @Shervan360

    You can use find function (Ctrl+F shortcuts) to find all ".Pages" in current project and then replace them to ".My".

    Refer to the following steps:

    1.Create a Razor Page application. Change the root directory and change the Pages folder to "My".

    2.Press the Ctrl + F shortcuts, the in the "Find..." textbox, enter the ".Pages", and select the "Current project" option, after that select the "Find All" option.

    User's image

    After that, it will show all files which using the ".Pages":

    User's image

    3.To change the ".Pages" to ".My", you can expand the search panel, and in the replace textbox, enter the ".My", after that click the Replace All icon to replace all text.

    User's image

    After that, it will show the following notification window.

    User's image

    And if you check the relates page, you can see the related page's namespace has already changed.

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments