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 entir开发者_如何学Ce 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!
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.
I came across this article whilst looking to fix this myself.
There were 2 problems that I noticed when the runat=server attribute was set... 1) carriage returns between meta tags were removed so all meta tags were bunched up together on one line 2) the title tag was pretty messed up (DVD described this perfectly)
The problem I had was that I needed to dynamically set the page title, meta description and meta keywords. To dynamically set the page title, the runat=server attribute needs to be set.
My solution;
- Remove the runat=server attribute from the head tag
- Add the runat=server attirbute to the title tag
- In code behind, reference the title tag and set the .InnerText property
Hope this helps.
精彩评论