tags:

views:

25

answers:

2

I'm new to .net, and I've noticed that when viewing my HTML source code generated by a .net application the carriage returns are removed from the head tag when it has runat="server" attribute on it.

I remove the runat="server" and the returns... return.

This really looks nasty when you have a few javascript and css files in your header because it ends up making the entire contents of the head tag 1 big line.

Just wondering if there's a way to control this or tell .net thru configuration not to mangle the output?

Thanks!

+1  A: 

You can create a ControlAdapter. Add a file Head.Browser to the App_Browsers directory with content:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlHead"
               adapterType="MyNamespace.MyHeadAdapter, MyAssembly" />
    </controlAdapters>
  </browser>
</browsers>

The adapter source:

    public class MyHeadAdapter: ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            // base.Render(writer);
            // do the fix here
        }
    }

PS: I've not tested this code, but have used almost the same to rewrite the form's action attriute with a custom class derived from HtmlTextWriter.

Mart
A: 

It's actually worse than just being a cosmetic problem because tags vital to good SEO (such as <title>) get messed up. So a properly formatted head tag gets rendered as:-

<head><title>

    My Page Title

</title></head>

Browsers still show the page title correctly but not all search engines parse the title tag correctly.

The first answer isn't a solution (for me at least). Anyone have one?

DVD