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

后端 未结 1 1355
野趣味
野趣味 2021-02-03 14:37

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

So when users at the main section of my website they get the default

相关标签:
1条回答
  • 2021-02-03 14:59

    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.

    0 讨论(0)
提交回复
热议问题