Routing and URL in ASP.NET Core Razor Pages

Shervan360 1,661 Reputation points
2023-08-21T07:32:55.92+00:00

Hello,

What is the ultimate form of URL in the following routing? I don't know when to use the forward slash.

https://localhost:7147/

@page "/Products/Add"

@page "/Products/Add/"

@page "Products/Add"

@page "./Products/Add"

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

Accepted answer
  1. Zhi Lv - MSFT 33,196 Reputation points Microsoft External Staff
    2023-08-22T02:58:58.5966667+00:00

    Hi @Shervan360

    For example, I create a Razor page application and add the Products folder under the Pages folder, then add the following page with the @page directive.

    User's image

    In my application, the application url is https://localhost:7215.

    Then, to access the Pages/Index.cshtml page, we can use https://localhost:7215 or https://localhost:7215/Index or https://localhost:7215/Index/

    To access the Pages/Add.cshtml page, we can use https://localhost:7215/Add or https://localhost:7215/Add/

    To access the Pages/Products/Index.cshtml page, we can use https://localhost:7215/Products or https://localhost:7215/Products/Index or we can add a forward slash at the end of the url like this: https://localhost:7215/Products/Index/.

    To access the Pages/Products/Create.cshtml page, we can use https://localhost:7215/Products/Create or https://localhost:7215/Products/Create/

    To access the Pages/Products/Add.cshtml page, we have to use https://localhost:7215/Products/Add/AAA/Add or https://localhost:7215/Products/Add/AAA/Add/

    So, from the above sample, we can know that, if we add the forward slash at the end of route template, like this: @page "/Products/Create"or @page "/Products/Create/", we can access products Create.cshtml by browsing to both https://localhost:7215/Products/Create or https://localhost:7215/Products/Create/.

    If we add a forward slash in front of the route template, it will use the template to specify an alternative route for a page that has no relationship with the file name. You can compare the request url between the Pages/Product/Create.cshtml page and Pages/Product/Add.cshtml page in the above sample, for the Add.cshtml page, since we didn't add the forward slash in front of the route template, it will append the template value at the end of the request url.

    The override route template should start with / or ~/. We will not using './' in the route template.

    For the './', we might use it in the hyperlink, for example, in the Pages/Index page, we can add the following hyperlink:

    <a asp-page="./Products/Create" >Product</a>
    

    The generate html elements as below: you can also delete the '.', it will get the same result:

    <a href="/Products/Create">Product</a>
    

    More detail information about Routing in Razor page, see Razor Pages Routing


    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.