Controlling Output Indentation in ASP.Net MVC

A colleague of mine is very hot on properly formatted and indented html that is delivered to the client browser. This means that the source of the page is easily human readable.

First, if I have a partial view that is used in several different areas of my site, should the rendering engine automatically format the padding for me (ala sets the formatting property to the XmlTextWriter)?

Secondly, a colleague of mine created several HtmlHelper extension methods to write the response. They all require the CurrentIndent parameter to be passed to them. I do not like it.

Can anyone help with this?

+2


source to share


4 answers


It's hard to maintain. If someone removes the outer element from HTML, would anyone want to update the CurrentIndent values ​​in code? Most developers these days usually view their HTML through Firebug anyway, which automatically formats the indented markup.



If you really want to post-process your HTML through a formatting filter, give it a try . NET port of HTML Tidy .

+6


source


The browser doesn't care how beautiful the HTML indentation is. What's even more, deeply nested (and thus heavily indented), HTML adds a small overhead to the page (in terms of byte load). Of course, you can always compress the response, and well HTML indentation is better maintained.



+3


source


Even if for some crazy reason it HAS TO should be indented "correctly", it shouldn't be done in the way your colleague suggests.

The HttpModule attached to the ReleaseRequestState

object's event HttpApplication

should do the trick. And of course, you will need to create a filter that handles this indentation.

public class IndentingModule: IHttpModule {

    public void Dispose() {
    }

    public void Init(HttpApplication context) {
        context.ReleaseRequestState += 
            new EventHandler(context_ReleaseRequestState);
    }

    void context_ReleaseRequestState(object sender, EventArgs e) {
        HttpApplication app = (HttpApplication)sender;
        app.Response.Filter = new IndentingFilter(app.Response.Filter)
    }
}

      

+3


source


Instead of wasting time implementing the right fallback solution that would affect all HTTP requests (thereby increasing CPU usage and bandwidth), just suggest to your colleague that he is using an HTML decoder. So one person who cares about it is the one who pays for it.

This Firefox plugin is an HTML validator that also includes a beautification feature. See the documentation here .

+2


source







All Articles