Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:51:30+00:00 2026-05-25T16:51:30+00:00

I wonder if it’s possible to wrap dynamically add layout to partial views based

  • 0

I wonder if it’s possible to wrap dynamically add layout to partial views based on routing.

For example:

  • http://my.app.com/Controller/GetForm – returns a partial view
  • http://my.app.com/Test/Controller/GetForm – returns the same partial view, but wrapped in _Layout.cshtml

The point here is that I don’t want to create another view with duplicate functionality, but rather set Layout property dynamically.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:51:31+00:00Added an answer on May 25, 2026 at 4:51 pm

    Try this:

    Action

    public ActionResult Index() {
        ViewBag.Layout = null;
    
        object layout;
        if (RouteData.Values.TryGetValue("layout", out layout)) {
            switch(layout.ToString())
            {
                case "Layout1":
                    ViewBag.Layout = "~/Views/Shared/_Layout.cshtml";
                    break;
                // ...
            }
        }
    
        return PartialView("Test");
    }
    

    Markup

    @{
         Layout = ViewBag.Layout;
    }
    
    <div>This is the test page...</div>
    

    Routing config

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
    
    routes.MapRoute(
        "DefaultWithLayout", // Route name
        "{layout}/{controller}/{action}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
    }
    

    Or just

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{layout}", // URL with parameters
        new { controller = "Home", action = "Index", layout = UrlParameter.Optional, id = UrlParameter.Optional }); // Parameter defaults
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if it is possible to get MatchCollection with all matches even if
I wonder how it would be possible to implement a positional inverted index in
I wonder whether it is possible to change the display server IP of a
Wonder if this possible. Saw many posts on adding watermark after the pdf is
I wonder if it is possible to scale text in the x-axis?
I wonder if it is possible, to stop Maven 3.0 downloading old libraries and
Wonder if anyone could shed some light on this http://jsfiddle.net/orbitalmedia/ZrxBb/7/ Basically I'm trying to
I wonder if it's possible to save all files in a Visual Studio 2008
I wonder if anyone knows about WAFL (Write Anywhere File Layout), or a link
i wonder if it is possible to auto mock a container in MOQ without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.