有什么方法可以指定整个 Controller 使用特定的布局吗?我知道您可以在 ViewStart 中分配布局,viewStart 是否可以知道正在使用什么 Controller ?
我的目标是拥有两种管理布局,其中一种在您使用管理 Controller 中的任何内容时带有额外的导航栏。
最佳答案
您可以编写自定义操作过滤器:
public class LayoutAttribute : ActionFilterAttribute
{
private readonly string _layout;
public LayoutAttribute(string layout)
{
_layout = layout;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = _layout;
}
}
}
然后用它装饰你的 Controller ,并且该 Controller 的所有操作(显然返回 View 结果)将使用你指定的布局:
[Layout("_SimpleLayout")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}
关于asp.net-mvc - 为整个 Controller 分配布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11588367/