Search code examples
c#asp.net-corehttp-redirectrazorrazor-pages

Redirecting To (Main Page) Razor Page without changing Address


newly learning razor

app.MapGet("/", () => Results.Redirect("/Index"));

redirectings to page Index in Pages Folder. web browser address bar seems with route name like this:

http://localhost:5013/Index

is there a way to redirect to Index Page withot seeing Route Name? something like:

http://localhost:5013

Solution

  • Actually, this is related with how you set the route for your page.

    For example, if you set the route as below inside the index page, then the default page for the / is the index. There is no need to use mapget again.

    @page "/"
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>
    

    Also if you want to set some custom route for specific page, you could use AddPageRoute method directly inside the page options.

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