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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:23:21+00:00 2026-06-03T07:23:21+00:00

I have a few Razor pages with lots of conditional logic, loops, partial views

  • 0

I have a few Razor pages with lots of conditional logic, loops, partial views etc. It’s easy to keep the output markup semantically correct, but more difficult to format it with the right indentation and line breaks. How could I do this automatically at runtime? Is there a module or Razor extension?

Please don’t say Right click -> Format selection. To be clear, I want to avoid having to think about formatting when I write the Razor pages. I want my Razor markup to be laid out in a way that makes sense to developers (indenting inside server-side blocks, for example) – but the rendered HTML to be ‘prettified’ for the odd person who clicks ‘View source’. (I’m not worried about increasing the size of the output because I’m using gzip/deflate.)

  • 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-03T07:23:22+00:00Added an answer on June 3, 2026 at 7:23 am

    You can use a library like TidyNet ( http://sourceforge.net/projects/tidynet/ ), an implement an ActionFilter:

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Result is ViewResult)
        {
            var tidy = new Tidy
                {
                    Options =
                        {
                            DocType = DocType,
                            DropFontTags = DropFontTags,
                            LogicalEmphasis = LogicalEmphasis,
                            XmlOut = XmlOut,
                            Xhtml = Xhtml,
                            IndentContent = IndentContent,
                            HideEndTags = HideEndTags,
                            MakeClean = MakeClean,
                            TidyMark = TidyMark,
                        }
                    };
    
            filterContext.RequestContext.HttpContext.Response.Filter =
                new HtmlTidyFilter(filterContext.RequestContext.HttpContext.Response.Filter, tidy);
        }
    }
    

    The algorithm of the filter:

    public override void Write(byte[] buffer, int offset, int count)
    {
        var data = new byte[count];
        Buffer.BlockCopy(buffer, offset, data, 0, count);
        string html = Encoding.Default.GetString(buffer);
    
        using (var input = new MemoryStream())
        {
            using (var output = new MemoryStream())
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(html);
                input.Write(byteArray, 0, byteArray.Length);
                input.Position = 0;
                _tidy.Parse(input, output, new TidyMessageCollection());
    
                string result = Encoding.UTF8.GetString(output.ToArray());
    
                byte[] outdata = Encoding.Default.GetBytes(result);
                _stream.Write(outdata, 0, outdata.GetLength(0));
            }
        }
    } 
    

    And then you just plug it into the Controller:

    [TidyHtml]
    public class AnyController : Controller
    

    Voilá! 😉

    I learned it from this source: http://blog.aquabirdconsulting.com/2009/10/28/asp-net-mvc-clean-html/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Razor, I have a few that's located in ~/areas/shared/views/shared/_partialName.cshtml . If I do
I have few FOREACH loops in php; $c1 = 1; $c2 = 1; $c3
On my site I have quite a few content pages. Most of them a
I have written a few razor helpers and these helpers use functions that include
I have standard ASP.NET 4 application. Inside this application there are also few razor
I have few question in this regard When you create an internet page, does
We have few components like libraries dlls When initially created I ran the following
I have few nested DIVs at page. I want to add event only for
I have few small basic problems : How to format : int i =
I have few Question for which I am not able to get a proper

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.