是否有可能使用基于关路线的MVC4不同的布局? [英] Is it possible to use different layouts in MVC4 based off of routes?

查看:110
本文介绍了是否有可能使用基于关路线的MVC4不同的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站的一个单独部分(路由),我想用不同的布局/ CSS等。

I have a separate section (route) of my website that I would like to use a different layout/css etc.

所以,当用户在我的网站的主要部分,他们得到的默认布局。
但是,当他们登录并去商店,店内部分(路由)使用不同的布局/ CSS。

So when users at the main section of my website they get the default layout. But when they log in and go to the store, the store section (route) uses a different layout/css.

所以...


  • www.blahblahblah.com /

  • www.blahblahblah.com/admin /

  • www.blahblahblah.com/home/contactus /

... ...所有使用默认_layout

...all use the default _Layout

但是...


  • www.blahblahblah.com/store /

  • www.blahblahblah.com/store/admin /

...使用_LayoutStore

...use _LayoutStore

我已经看到了这个基础在这里下车的角色( http://forums.asp.net/t完成/1653362.aspx/1 )和这里(如何使用多个布局MVC 3?),但我不想这样做。我需要我的布局选择基于关闭的客户需要什么路线(又名查看他们在里面)。

I've seen this done based off of roles here (http://forums.asp.net/t/1653362.aspx/1) and here (How to use multiple Layout in MVC 3?) BUT I don't want to do that. I need my layout selection based off of what route the customer takes (aka view they're inside).

感谢您预先的任何及所有的帮助。

Thank you in advance for any and all help.

推荐答案

你有没有看使用任何给定的视图文件夹中的 _ViewStart.cshtml 文件?

Have you looked at using _ViewStart.cshtml files within any given view folder?

如果这不是你在寻找什么,你想在路由中的值来决定使用,你可以尝试创造一些辅助方法将返回布局要使用的布局:

If that's not exactly what you're looking for and you want the values in the routing to determine which layout to use you could try creating some helper method that would return the layout to use:

    public static class LayoutHelper
    {
        public static string GetLayout(RouteData data, string defaultLayout = "")
        {
            if (data.Values["action"] == "edit")
                return "~/views/shared/_AdminLayout.cshtml";

            return defaultLayout;
        }
    }

然后你可以从你的浏览调用它像这样:

Then you can call it from your View like so:

@{
    Layout = LayoutHelper.GetLayout(
        Request.RequestContext.RouteData,
        "~/views/shared/_layout.cshtml");
}

但在我看来,如果你在查看/存储文件夹中创建一个 _ViewStart.cshtml 文件,其中包含店面布局,你会好到哪里去。

But it seems to me that if you created a _ViewStart.cshtml file in the Views/Store folder containing the store layout you would be good to go.

这篇关于是否有可能使用基于关路线的MVC4不同的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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