如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局? [英] How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

查看:25
本文介绍了如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的应用程序中有 2 个单独的布局.假设一个用于网站的公共部分,另一个用于会员端.

I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side.

为简单起见,假设每个站点的所有逻辑都整齐地包装到 2 个不同的控制器中.

For simplicity, let's say all the logic for each of these sites is wrapped neatly into 2 distinct controllers.

  • 公共控制器
  • 员工控制器

并且他们每个人都有一个对应的布局,用于每个视图下的所有视图.

And that they each have a corresponding Layout for all the View under each.

  • _PublicLayout.cshtml
  • _StaffLayout.cshtml

如何使用 _ViewStart.cshtml 文件指定公共"下的所有视图/操作使用 PublicLayout 和Staff"下的所有内容;使用 StaffLayout?

How do I use the _ViewStart.cshtml file to specify that all Views / Actions under "Public" use the PublicLayout and everything under "Staff" uses the StaffLayout?

推荐答案

您可以将 _ViewStart.cshtml 文件放在 /Views/Public 文件夹中,该文件将覆盖/Views 文件夹中的默认设置并指定所需的布局:

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:

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

以此类推,您可以将另一个 _ViewStart.cshtml 文件放入 /Views/Staff 文件夹中:

By analogy you could put another _ViewStart.cshtml file inside the /Views/Staff folder with:

@{
    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.

更新:

根据评论部分的要求,这是一个选择母版页的操作过滤器示例:

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();
}

这篇关于如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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