Home > Software design >  How to link a razor page in asp.net core mvc web application
How to link a razor page in asp.net core mvc web application

Time:07-07

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

CodePudding user response:

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://docs.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>

CodePudding user response:

E.g You have this in your layouts page

<a asp-area="" asp-controller="Home"asp- action="page">Privacy

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

  • Related