I'm currently trying to extend an existing ASP.NET Core MVC project by a Razor page (since several tutorial videos claim that MVC, API, Razor and Blazor can coexist in the same project - but none of them shows how it's done).
I already figured out I need to extend Startup.cs by
services.AddRazorPages();
and
app.UseEndpoints(endpoints =>
{
// This was here already
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// I added this
endpoints.MapRazorPages();
});
I tried simply adding a razor page "Test" to the Views folder, extending the _Layout.cshtml by
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Test">Test</a>
</li>
then extending HomeController by
public IActionResult Test()
{
return View();
}
However, this causes several issues with breakpoints not being hit, or the ViewData dictionary being null (with the identical code working in a pure Razor Page project), probably since it tries treating the Razor Page as an MVC view or something.
I've also tried adding something like
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Home/Test">Test</a>
</li>
to the layout instead, but this produces an URL like
https://localhost:5001/?page=%2FHome%2FTest
when clicking the navbar item.
I can perfectly have both things in separate VS projects, but isn't there a way to use both of them in a single VS project and a single layout?
If you want to try it out before answering, use the following steps:
- Create a new project/solution in Visual Studio 2019
- Select "ASP.NET Core Web Application" as project template
- Click "Create" and select "Web Application (Model-View-Controller)" as template with default settings
- Add Razor Support in Startup.cs
- Try to make a simple razor page working in this project
