Is it possible to use different layouts in MVC4 based off of routes?

ぐ巨炮叔叔 提交于 2019-12-03 01:38:41

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!