asp.net-mvc 如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5161380/
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
How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?
提问by Justin
I would like to have 2 separate Layouts in my application. Let say one is for the Public section of the website and the other is for the Member side.
我想在我的应用程序中有 2 个单独的布局。假设一个用于网站的公共部分,另一个用于会员端。
For simplicity lets say all the logic for each of theses sites is wrapped neatly into 2 distinct controllers.
为简单起见,假设每个站点的所有逻辑都整齐地包装到 2 个不同的控制器中。
- PublicController
- StaffController
- 公共控制器
- 职员控制器
And that they each have a corresponding Layout for all the View under each.
并且他们每个人都有一个对应的布局,用于每个视图下的所有视图。
- _PublicLayout.cshtml
- _StaffLayout.cshtml
- _PublicLayout.cshtml
- _StaffLayout.cshtml
How do I use the _ViewStart.cshtml file to specify that all View's / Action under "Public" use the PublicLayout and everything under "Staff" use the StaffLayout?
如何使用 _ViewStart.cshtml 文件指定“Public”下的所有视图/操作使用 PublicLayout 而“Staff”下的所有内容都使用 StaffLayout?
Thanks!
谢谢!
回答by Darin Dimitrov
You could put a _ViewStart.cshtml
file inside the /Views/Public
folder which would override the default one in the /Views
folder and specify the desired layout:
您可以在_ViewStart.cshtml
文件/Views/Public
夹中放置一个文件,该文件将覆盖文件夹中的默认/Views
文件并指定所需的布局:
@{
Layout = "~/Views/Shared/_PublicLayout.cshtml";
}
By analogy you could put another _ViewStart.cshtml
file inside the /Views/Staff
folder with:
以此类推,您可以将另一个_ViewStart.cshtml
文件放入/Views/Staff
文件夹中:
@{
Layout = "~/Views/Shared/_StaffLayout.cshtml";
}
You could also specify which layout should be used when returning a view inside a controller action but that's per action:
您还可以指定在控制器操作中返回视图时应使用哪种布局,但这是每个操作:
return View("Index", "~/Views/Shared/_StaffLayout.cshtml", someViewModel);
Yet another possibility is a custom action filter which would override the layout. As you can see many possibilities to achieve this. Up to you to choose which one fits best in your scenario.
另一种可能性是自定义操作过滤器,它将覆盖布局。正如您所看到的,实现这一目标的可能性有很多。由您来选择最适合您的场景。
UPDATE:
更新:
As requested in the comments section here's an example of an action filter which would choose a master page:
根据评论部分的要求,这里有一个选择母版页的操作过滤器示例:
public class LayoutInjecterAttribute : ActionFilterAttribute
{
private readonly string _masterName;
public LayoutInjecterAttribute(string masterName)
{
_masterName = masterName;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = _masterName;
}
}
}
and then decorate a controller or an action with this custom attribute specifying the layout you want:
然后用这个自定义属性装饰一个控制器或一个动作,指定你想要的布局:
[LayoutInjecter("_PublicLayout")]
public ActionResult Index()
{
return View();
}
回答by shaijut
回答by Anil Sharma
This method is the simplest way for beginners to control Layouts rendering in your ASP.NET MVC application. We can identify the controller and render the Layouts as par controller, to do this we can write our code in _ViewStart file in the root directory of the Views folder. Following is an example shows how it can be done.
此方法是初学者在 ASP.NET MVC 应用程序中控制布局呈现的最简单方法。我们可以识别控制器并将布局渲染为标准控制器,为此我们可以在 Views 文件夹的根目录下的 _ViewStart 文件中编写我们的代码。以下是一个示例,展示了如何做到这一点。
@{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
string cLayout = "";
if (controller == "Webmaster")
cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";
else
cLayout = "~/Views/Shared/_Layout.cshtml";
Layout = cLayout;
}
Read Complete Article here"How to Render different Layout in ASP.NET MVC"
在此处阅读完整文章“如何在 ASP.NET MVC 中呈现不同的布局”