skip to Main Content

I am working with ASP.net 5.0(deprecated I know) so I want to add a webpage(razor page (.cshtml)) and link it to the nav bar as an nav item…. In HTML its just so simple use anchor tag and link it but no its not working in asp.net, so anybody here can provide details or documentation for doing this, I am a beginner so please any help would be appreciated

2

Answers


  1. E.g You have this in your layouts page

    <a class="nav-link text-dark" asp-area="" asp-controller="Home"asp-    action="page">Privacy</a>
    

    You will need a controller named Home for that webpage all you need to do is create a method in the Home controller that routes to that page

     public IActionResult page()
      {
            return View();
      }
    

    Note the should be located razor page should be in Home folder(The name of your controller) located in Views folder

    Login or Signup to reply.
  2. Create a folder named Pages and place your Razor page in that. Ensure that you have added the Razor Pages services and endpoints in Startup:

    services.AddRazorPages();
    
    app.UseEndpoints(endpoints =>
    {
         endpoints.MapRazorPages();
    });
    

    (https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio)

    In the anchor tag helper, make sure you use the asp-page attribute and pass in the relative path of your page rooted in the Pages directory, without the file extension. It should look like this:

    <a asp-page="/YourPage">Click</a>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search