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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:57:22+00:00 2026-06-06T16:57:22+00:00

I want to manipulate HTML using a custom view engine like this: protected override

  • 0

I want to manipulate HTML using a custom view engine like this:

protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
{
    if (viewContext.Controller is MyController)
    {
       .. alter the html then write to writer
    } else {
        base.RenderView(viewContext, writer, instance);
    }
}

RenderView gets called for the view and each partial view, but I can’t figure out a reliable way to determine whether it’s rendering the outer view or a partial view.

The type of the writer object is HttpWriter for a partial view and StringWriter for the outer view. This seems like a bad way to test it, but so far it’s the only concrete thing I can find

The instance tells me the view or partial view name, but checking a string to see if I’m rendering a partial view or not seems brittle.

Is there any more direct way to determine if it’s a partial view or not?

  • 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-06-06T16:57:23+00:00Added an answer on June 6, 2026 at 4:57 pm

    You could pass a boolean parameter to your custom view from your custom view engine:

    public class MyRazorView : RazorView
    {
        private readonly bool _isPartial;
    
        public MyRazorView(ControllerContext controllerContext, string viewPath, string layoutPath, bool runViewStartPages, IEnumerable<string> viewStartFileExtensions, IViewPageActivator viewPageActivator, bool isPartial)
            : base(controllerContext, viewPath, layoutPath, runViewStartPages, viewStartFileExtensions, viewPageActivator)
        {
            _isPartial = isPartial;
        }
    
        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            if (viewContext.Controller is MyController)
            {
                if (_isPartial)
                {
                    // it's a partial
                }
                else
                {
                    // it's the main view
                }
            } 
            base.RenderView(viewContext, writer, instance);
        }
    }
    
    public class MyRazorViewEngine : RazorViewEngine
    {
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new MyRazorView(
                controllerContext, 
                partialPath, 
                null, 
                false, 
                base.FileExtensions, 
                base.ViewPageActivator, 
                true
            );
        }
    
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new MyRazorView(
                controllerContext, 
                viewPath, 
                masterPath, 
                true, 
                base.FileExtensions, 
                base.ViewPageActivator, 
                false
            );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Python ElementTree module to manipulate HTML. I want to emphasize
I have a HTML Table which in which i want to manipulate using JQuery
I have a table of values like this: http://www.conversiontable.org/clothingsizeconversiontable.html and I want to declare
I am posting some data using ajax. I want to manipulate that data and
This may be a simple question. I need to access an html element using
I'm using the HTML Agility Pack to manipulate and edit a HTML document. I
I have an ASP.NET gridview I want to manipulate using JavaScript/JQuery. The problem I
I have a HTML structure like this: <div class=votes> <b>5</b> Votes <a id=vote-' +
I know that using Regular expressions to parse or manipulate HTML/XML is a bad
I want to write an application using jsTree that would display and manipulate directory

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.