简体   繁体   English

如何在没有默认布局的情况下在 aspnet core mvc 上映射默认控制器

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

I created a .NET Core 3.1 ASPNET project.我创建了一个 .NET Core 3.1 ASPNET 项目。 By default, the startup page us the Home Controller/Index page.默认情况下,启动页面是 Home Controller/Index 页面。

 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.但是我想创建一个登录页面,所以我创建了一个带有新操作的新控制器,并传递了参数,但它不是仅渲染登录页面,而是在布局项目中的“@RenderBody()”内部进行渲染. On .NET MVC5 it only rendered the page that I set.在 .NET MVC5 上,它只呈现我设置的页面。 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登录页面不与布局分开呈现

在此处输入图片说明

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:如果您查看 Views 文件夹,将会有一个名为 _ViewStart.cshtml 的文件,其中包含以下代码:

@{
    Layout = "_Layout";
}

_ViewStart.cshtml runs for every view, so by default it sets the layout page to _Layout.cshtml. _ViewStart.cshtml 为每个视图运行,因此默认情况下它将布局页面设置为 _Layout.cshtml。

There are 2 options:有2个选项:

  • Remove the code from _ViewStart.cshtml, meaning the Layout needs to be explicitly set in each view.从 _ViewStart.cshtml 中删除代码,这意味着需要在每个视图中显式设置Layout
  • In your login view, set Layout to null .在您的登录视图中,将Layout设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM