Search code examples
asp.net-corerazorrazor-pages

Change default Url when start ASP.NET Core Razor pages web app


When run web, it running with url http://localhost:5000/.

How can I change that url to http://localhost:5000/ABC when starting web and also it will redirect to Index.cshtml in ABC folder (not Pages folder).

I try change it in Program.cs like this:

razorPage = builder.Services.AddRazorPages(options =>
            {
                options.Conventions.AddPageRoute("/ABC/Index", "");
            })

but this is not working.


Solution

  • you should change AddPageRoute as follows , also change default pages directory as well :

    builder.Services.AddRazorPages(options =>
    {
       options.Conventions.AddPageRoute("/index", "/abc/index");
    
    }).AddRazorPagesOptions(opt => opt.RootDirectory = "/ABC");
    

    and if you want change default home page route you can add end point as follows :

    app.MapGet("/", context =>
    {
       context.Response.Redirect("/abc/index");
       return Task.CompletedTask;
    });
    
    app.MapRazorPages();