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 6697685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:29:21+00:00 2026-05-26T06:29:21+00:00

In a .NET Razor Web Application i’m trying to programmatically set the Layout .

  • 0

In a .NET Razor Web Application i’m trying to programmatically set the Layout. I can not use _ViewStart.cshtml and don’t wont to set the @{ Layout = "..." } on every page. This is what I have come up with:

A base WebViewPage class:

public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T>
{
    private object _layout;

    public new dynamic Layout { get { return _layout; } }

    public override void InitHelpers()
    {
        base.InitHelpers();
        _layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
    }
}

And in the application web.config I specify all view to use this base page. But the Layout is never used it seems. What could be wrong here?

  • 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-26T06:29:21+00:00Added an answer on May 26, 2026 at 6:29 am

    The WebViewPage class inherits from WebPageBase that has a property named Layout like:

    public override string Layout { get; set; }
    

    You can override the Layout property, or change your _layout logic to achieve your purpose. For example:

    public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T> {
    
        // set this modifier as protected, to make it accessible from view-pages
        protected string _layout{
            get {
                return base.Layout;
            }
            set {
                base.Layout = value;
            }
        }
    
        public override void InitHelpers() {
            base.InitHelpers();
            _layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
        }
    }
    

    and/or in a view-page, you can set it too:

    @{
        _layout = "_Your_Special_Layout.cshtml";
    }
    

    UPDATE: using a flag to avoid stack-over-flow in assigning _layout more that once:

    public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T> {
    
        public bool LayoutAssigned {
            get {
                return (ViewBag.LayoutAssigned == true);
            }
            set {
                ViewBag.LayoutAssigned = value;
            }
        }
    
        // set this modifier as protected, to make it accessible from view-pages
        protected string _layout{
            get {
                return base.Layout;
            }
            set {
                base.Layout = value;
            }
        }
    
        public override void InitHelpers() {
            base.InitHelpers();
            if(!LayoutAssigned) {
                _layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
                LayoutAssigned = true;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built an ASP.NET MVC 3 web application (with exlusively Razor/cshtml pages) that
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
I am creating a new web application using ASP.NET MVC3 Razor and HTML 5.
Using MVC3 and Razor View engine, I created a VB.NET web application in VS
I am trying to use Meleze.Web - 1.0.0 to minify the razor views of
I have an ASP.NET MVC 3 (Razor) Web Application, with a particular page which
I have an ASP.NET MVC3 web application with C# and Razor. Through a form
I am developing an ASP.NET MVC 3 web application using Razor and C#. I
Can I create not .cshtml file ( .txt , .js ...), include Razor operators
I am running the following code on an ASP.NET razor page (cshtml) and am

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.