MVC3 Razor 根据不同页面使用不同Layout
_ViewStart.cshtml运行于每一Page前, 所以通常在这里先设置Layout
下面代码为特定的controller指定Index,Edit,Create的模板
即Index对应 _Index_Layout, Edit对应_Index_Layout...
@{
var controller = ViewContext.RouteData.Values["controller"].ToString().ToLower();
var action = ViewContext.RouteData.Values["action"].ToString().ToLower();
string[] lController = new string[] { "user", "news", "mail" };
string[] lAction = new string[] { "index", "edit", "create" };
if (lController.Contains(controller)){
if (lAction.Contains(action))
{
Layout = "~/Views/Shared/_" + action + "_Layout.cshtml";
}else if (action == ""){
Layout = "~/Views/Shared/_Index_Layout.cshtml";
}
}
}