ASP.NET: Page HTML head rendering

Posted by Fabian on Stack Overflow See other posts from Stack Overflow or by Fabian
Published on 2010-04-01T10:54:08Z Indexed on 2010/04/01 11:03 UTC
Read the original article Hit count: 193

Filed under:
|

I've been trying to figure out the best way to custom render the <head> element of a pag to get rid of the extra line breaks which is caused by <head runat="server">, so its properly formatted.

So far the only thing i've found which works is the following:

    protected override void Render(HtmlTextWriter writer)
    {
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        base.Render(htmlTextWriter);
        htmlTextWriter.Close();
        string html = stringWriter.ToString();
        string newHTML = html.Replace("\r\n\r\n<!DOCTYPE", "<!DOCTYPE")
                             .Replace("\r\n<html", "<html")
                             .Replace("<title>\r\n\t", "<title>")
                             .Replace("\r\n</title>", "</title>")
                             .Replace("</head>", "\n</head>");

        writer.Write(newHTML);
    }

I define my had tag like

Now i have 2 questions:

  1. How does the above code affect the performance (so is this viable in an production environment)?
  2. Is there a better way to do this, for example a method which i can override to just custom render the <head>?

Oh yeah ASP.NET MVC is not an option.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#