asp.net-mvc 强制所有区域使用相同的布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13277126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:23:29  来源:igfitidea点击:

Force all Areas to use same Layout

asp.net-mvcrazorasp.net-mvc-routingasp.net-mvc-areas

提问by Mikhail

I have the following project structure:

我有以下项目结构:

  • /Views/Shared/_Layout;

  • /Areas/Area1/Views/ControllerName/Index;

  • /视图/共享/_布局;

  • /Areas/Area1/Views/ControllerName/Index;

...

...

  • /Areas/AreaN/Views/ControllerName/Index.
  • /Areas/AreaN/Views/ControllerName/Index.

Is there any way to force all areas to use the _Layoutas a base layout?

有没有办法强制所有区域使用_Layout作为基本布局

Is there any way to do it without adding the _ViewStartfile (for example, via the routing configuration)?

有没有办法在不添加_ViewStart文件的情况下做到这一点(例如,通过路由配置)?

See Also:

也可以看看:

How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局?

回答by Jupaol

You just have to add a file named:

您只需要添加一个名为:

_ViewStart.cshtml

Under each area views folder:

在每个区域视图文件夹下:

/Areas/Area1/Views/_ViewStart.cshtml

And edit the file to point to the root layout like this:

并编辑文件以指向根布局,如下所示:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

In order for this to work, you do not have to specify a value in the view's layout property, if you do, you would be overriding the global layout

为了使其工作,您不必在视图的布局属性中指定值,如果这样做,您将覆盖全局布局

Note: As Tony mentioned, you couldedit each view's layout property to point to the root layout, however this is not the recommended way to do it since you would be coupling your views with your layout and change it would be painful

注意:正如 Tony 提到的,您可以编辑每个视图的布局属性以指向根布局,但是这不是推荐的方法,因为您会将视图与布局耦合并更改它会很痛苦

Edit 1

编辑 1

If you would like to use code to set the default view's layout, perhaps you should consider writing a custom view engine.

如果您想使用代码来设置默认视图的布局,也许您应该考虑编写自定义视图引擎。

Try to google about custom RazorViewEngineand RazorView

尝试谷歌关于自定义RazorViewEngineRazorView

This article could be a good start point

这篇文章可能是一个很好的起点

http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

I have not done something like this but I hope I'm pointing you in the right direction

我没有做过这样的事情,但我希望我能指出你正确的方向

回答by Rob Campbell

Expanding on the answer by Jupaol....

扩展 Jupaol 的答案....

At least in VS2013, the _ViewStart.cshtml file is added by default when creating the area, so it's already there, and you can change the contents as he notes to point to the root _Layout.cshtml. You can then remove the _Layout.cshtml in the area, since it is no longer used (and a potential source of confusion now)

至少在VS2013中,创建区域时默认添加了_ViewStart.cshtml文件,所以它已经存在了,你可以按照他的注释修改内容指向根_Layout.cshtml。然后您可以删除该区域中的 _Layout.cshtml,因为它不再使用(并且现在是潜在的混乱来源)

However, by so doing any routing performed in that root _Layout.cshtml will need to consider areas.
The default _Layout.cshtml has a number of ActionLink helpers that need a slight modification:

但是,通过这样做,在根 _Layout.cshtml 中执行的任何路由都需要考虑区域。
默认的 _Layout.cshtml 有许多 ActionLink 助手需要稍作修改:

Add the RouteValueDictionary param to any ActionLink calls by setting Area="". Note that empty string refers to the root level. This will allow these links to work correctly when invoked from within an area, still work when invoked from the root.

通过设置 Area="" 将 RouteValueDictionary 参数添加到任何 ActionLink 调用。请注意,空字符串是指根级别。这将允许这些链接在从区域内调用时正常工作,从根调用时仍然有效。

e.g.:

例如:

<li>@Html.ActionLink("Home", "Index", "Home", new { Area = "" }, null)</li>

回答by Tony

You specify a layout using:

您可以使用以下命令指定布局:

@{ Layout = "_Layout"; }

@{ 布局 = "_布局"; }

If you want to make this easier to change all at once. Perhaps you could just set it as a view bag variable and pass it in on the controller. To make it even easier you could create a base controller that the other controllers inherit from and have it assign the layout to the view bag there.

如果你想让这更容易一次改变。也许您可以将其设置为视图包变量并将其传递给控制器​​。为了使它更容易,您可以创建一个其他控制器继承的基本控制器,并将布局分配给那里的视图包。

Not sure why routing would need to change or perhaps I am not understanding. Hope this helps :)

不知道为什么路由需要改变,或者我可能不理解。希望这可以帮助 :)