强制所有领域使用相同的布局 [英] Force all Areas to use same Layout

查看:137
本文介绍了强制所有领域使用相同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下项目结构:


  • /查看/共享/ _layout;

  • /Views/Shared/_Layout;

/地区/区域1 /浏览/ ControllerName /索引;

/Areas/Area1/Views/ControllerName/Index;

...


  • /地区/ AreaN /查看/ ControllerName /索引。

有没有办法强制所有地区使用 _layout 基地布局

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

有没有办法做到这一点不加入 _ViewStart 文件(例如,通过配置路由)?

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

另请参见:

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

推荐答案

您只需要添加一个文件名为:

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 could edit 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

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

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

试着google一下定制 RazorViewEngine RazorView

Try to google about custom RazorViewEngine and RazorView

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

This article could be a good start point

<一个href=\"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

这篇关于强制所有领域使用相同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆