Nathiel Paulino
Nathiel Paulino

Reputation: 544

how to map a default controller on aspnet core mvc without the default layout

I created a .NET Core 3.1 ASPNET project. By default, the startup page us the Home Controller/Index page.

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

But I want to make a Login Page, so I Created a New Controller with a new Action and I passed on the parameter, but instead of ONLY render the Login Page, it renders inside of the "@RenderBody()" from the layout project. On .NET MVC5 it only rendered the page that I set. I already tried with :

app.UseMvc(routes =>
{
   routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

And it's the same thing. The login page it's not rendered apart of the layout

enter image description here

Upvotes: 0

Views: 1352

Answers (1)

Johnathan Barclay
Johnathan Barclay

Reputation: 20354

This has nothing to do with routing.

If you look in your Views folder, there will be a file called _ViewStart.cshtml, containing this code:

@{
    Layout = "_Layout";
}

_ViewStart.cshtml runs for every view, so by default it sets the layout page to _Layout.cshtml.

There are 2 options:

  • Remove the code from _ViewStart.cshtml, meaning the Layout needs to be explicitly set in each view.
  • In your login view, set Layout to null.

Upvotes: 2

Related Questions