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

1

6

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 layout. But when they log in and go to the store, the store section (route) uses a different layout/css.

So...

  • www.blahblahblah.com/
  • www.blahblahblah.com/admin/
  • www.blahblahblah.com/home/contactus/

...all use the default _Layout

BUT...

  • www.blahblahblah.com/store/
  • www.blahblahblah.com/store/admin/

...use _LayoutStore

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.

Ochone answered 24/8, 2012 at 18:27 Comment(2)
take a look at Areas feature. You'll have Shared folder to each Area and you'll be able to apply different layouts to them.Mime
Actually, that's what I've settled with doing. I created another area for my store, since it's pretty much another section of my site. Thanks for the tip Thiago.Ochone
B
13

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.

Bicknell answered 24/8, 2012 at 18:31 Comment(5)
Well SHOOOoooot, that's an easy solution! Thanks man! In my site structure I now have: -Views/ ---Store/ -----Shared/ -------_Layout.html -----_Viewstart.html -----Index.html Because by default it seems to want to search the /Store/ and /Store/Shared/ directories for the Index.html file. How do I change where it looks for the index.html? Or other views for instance? Do I need to setup a new route?Ochone
Why do you have a Store/Shared folder? It seems you should go with Store/_Store.Layout.cshtml. I don't think MVC will search subfolders 2 levels deep from the root Views folder.Bicknell
Through my experiments/tests it actually always searches both the root directory and the "Shared" directory for files (such as "index" and "_layout") What I wanted to accomplish though was the same organization that is contained within views, but in a sub-directory called store. That way I could keep track of the different sections of the store easily.Ochone
If the layout isn't in the folder the current view resides in or in the shared folder you'll have to fully qualify the path like ~/store/shared/_layout.cshtmlBicknell
What I ended up doing was creating an "Area" for the Store in my project. It's worked out great for what I need.Ochone

© 2022 - 2024 — McMap. All rights reserved.