title: How to extend an ASP.NET Core MVC project by Razor Pages?
body: <p>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).</p>
  <p>I already figured out I need to extend Startup.cs by</p>
  <pre><code>services.AddRazorPages();
  </code></pre>
  <p>and</p>
  <pre><code>app.UseEndpoints(endpoints =&gt;
  {
      // This was here already
      endpoints.MapControllerRoute(
          name: &quot;default&quot;,
          pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
      // I added this
      endpoints.MapRazorPages();
  });
  </code></pre>
  <p>I tried simply adding a razor page &quot;Test&quot; to the <code>Views</code> folder, extending the <code>_Layout.cshtml</code> by</p>
  <pre><code>&lt;li class=&quot;nav-item&quot;&gt;
      &lt;a class=&quot;nav-link text-dark&quot; asp-area=&quot;&quot; asp-controller=&quot;Home&quot; asp-action=&quot;Test&quot;&gt;Test&lt;/a&gt;
  &lt;/li&gt;
  </code></pre>
  <p>then extending <code>HomeController</code> by</p>
  <pre><code>    public IActionResult Test()
      {
          return View();
      }
  </code></pre>
  <p>However, this causes several issues with breakpoints not being hit, or the <code>ViewData</code> dictionary being <code>null</code> (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.</p>
  <p>I've also tried adding something like</p>
  <pre><code>&lt;li class=&quot;nav-item&quot;&gt;
      &lt;a class=&quot;nav-link text-dark&quot; asp-area=&quot;&quot; asp-page=&quot;/Home/Test&quot;&gt;Test&lt;/a&gt;
  &lt;/li&gt;
  </code></pre>
  <p>to the layout instead, but this produces an URL like</p>
  <pre><code>https://localhost:5001/?page=%2FHome%2FTest
  </code></pre>
  <p>when clicking the navbar item.</p>
  <p>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?</p>
  <p>If you want to try it out before answering, use the following steps:</p>
  <ol>
  <li>Create a new project/solution in Visual Studio 2019</li>
  <li>Select &quot;ASP.NET Core Web Application&quot; as project template</li>
  <li>Click &quot;Create&quot; and select &quot;Web Application (Model-View-Controller)&quot; as template with default settings</li>
  <li>Add Razor Support in Startup.cs</li>
  <li>Try to make a simple razor page working in this project</li>
  </ol>