ASP.NET: Page HTML head rendering
- by Fabian
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:
How does the above code affect the performance (so is this viable in an production environment)?
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.