Server Removes Spacing Between Head Tags

Not really a problem, but it is a bit of an annoyance. And I haven’t been able to find relevant information through searches. Hopefully someone here can help. I’m very green when it comes to sever side scripting.

I have an ASPX page with a standard form. In the head I have meta tags, the title tag, and a link tag neatly ordered on their own lines. However, when viewing the source code after publishing to the server, the spacing between the tags is removed and it looks quite messy. (There are also <style> and <script> tags that follow, but they remain unaffected.)

I realize this has no practical effect on the site itself (in an SEO sense or otherwise). My project manager shows the source code to our clients to educate them on meta tags and page titles. It would help if it wouldn’t become jumbled like this.

I wonder if this is a common issue and if it’s possible to prevent through better coding practices.
Thanks for reading. Following are the relevant code snippets.

HTML as authored, with tags separated on their own lines:

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Welcome to Lawn Care Waukesha - Cut My Lawn. Cut My Lawn - Lawn Care Services has offered quality lawn cutting, fertilizing, aerating, and much more at affordable pricing since 2002! We currently offer lawn care service to Waukesha, Brookfield, Pewaukee, Menomonee Falls, and surrounding communities." />
<meta name="keywords" content="lawn cutting, lawn mowing, lawn care, fertilizing, aeration, mulching, shrub trimming, lawn mowing, edging, pruning, mulching, weed control, waukesha, Brookfield, Pewaukee, menomonee falls" />

<title>Lawn Care Waukesha — Cut My Lawn, Lawn Care Service</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

HTML after being processed by the sever, with all the tags running together:

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="description" content="Welcome to Lawn Care Waukesha - Cut My Lawn. Cut My Lawn - Lawn Care Services has offered quality lawn cutting, fertilizing, aerating, and much more at affordable pricing since 2002! We currently offer lawn care service to Waukesha, Brookfield, Pewaukee, Menomonee Falls, and surrounding communities." /><meta name="keywords" content="lawn cutting, lawn mowing, lawn care, fertilizing, aeration, mulching, shrub trimming, lawn mowing, edging, pruning, mulching, weed control, waukesha, Brookfield, Pewaukee, menomonee falls" /><title>
	Lawn Care Waukesha — Cut My Lawn, Lawn Care Service
</title><link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

I’m not sure it’s relevant, but here’s the script used to send the form (which I didn’t write, by the way).
It’s the final tag inside the page head:

    <script type="" runat="server">
        Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If Not Page.IsValid Then Exit Sub

            Dim SendResultsTo As String = "email"
            Dim smtpMailServer As String = "smtp"
            Dim smtpUsername As String = "email"
            Dim MailSubject As String = "subject"

            Try
                Dim txtQ As TextBox = Me.FormContent.FindControl("TextBoxQ")
                If txtQ IsNot Nothing Then
                    Dim ans As String = ViewState("hf1")
                    If ans.ToLower <> txtQ.Text.ToLower Or ans.ToUpper <> txtQ.Text.ToUpper Then
                        Me.CutMyLawnForm.ActiveViewIndex = 3
                        Exit Sub
                    End If
                End If

                Dim FromEmail As String = SendResultsTo
                Dim msgBody As StringBuilder = New StringBuilder()
                Dim sendCC As Boolean = False

            
                For Each c As Control In Me.FormContent.Controls
                    Select Case c.GetType.ToString
                        Case "System.Web.UI.WebControls.TextBox"
                            Dim txt As TextBox = CType(c, TextBox)
                            If txt.ID.ToLower <> "textboxq" Then
                                msgBody.Append(txt.ID & ": " & txt.Text & vbCrLf & vbCrLf)
                            End If
                            If txt.ID.ToLower = "email" Then
                                'FromEmail = txt.Text
                            End If
                            If txt.ID.ToLower = "subject" Then
                                MailSubject = txt.Text
                            End If
                        Case "System.Web.UI.WebControls.CheckBox"
                            Dim chk As CheckBox = CType(c, CheckBox)
                            If chk.ID.ToLower = "checkboxcc" Then
                                If chk.Checked Then sendCC = True
                            Else
                                msgBody.Append(chk.ID & ": " & chk.Checked & vbCrLf & vbCrLf)
                            End If
                            
                        Case "System.Web.UI.WebControls.RadioButton"
                            Dim rad As RadioButton = CType(c, RadioButton)
                            msgBody.Append(rad.ID & ": " & rad.Checked & vbCrLf & vbCrLf)
                        Case "System.Web.UI.WebControls.DropDownList"
                            Dim ddl As DropDownList = CType(c, DropDownList)
                            msgBody.Append(ddl.ID & ": " & ddl.SelectedValue & vbCrLf & vbCrLf)
                    End Select
                Next
                msgBody.AppendLine()
                
                msgBody.Append("Browser: " & Request.UserAgent & vbCrLf & vbCrLf)
                msgBody.Append("IP Address: " & Request.UserHostAddress & vbCrLf & vbCrLf)
                msgBody.Append("Server Date & Time: " & DateTime.Now & vbCrLf & vbCrLf)

                Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
                myMessage.To.Add(SendResultsTo)
                myMessage.From = New System.Net.Mail.MailAddress(FromEmail)
                myMessage.Subject = MailSubject
                myMessage.Body = msgBody.ToString
                myMessage.IsBodyHtml = False
                If sendCC Then myMessage.CC.Add(FromEmail)

                
                Dim basicAuthenticationInfo As New System.Net.NetworkCredential(smtpUsername, smtpPassword)
                Dim MailObj As New System.Net.Mail.SmtpClient(smtpMailServer)
                MailObj.Credentials = basicAuthenticationInfo
                MailObj.Send(myMessage)

                Me.CutMyLawnForm.ActiveViewIndex = 1
            Catch
                Me.CutMyLawnForm.ActiveViewIndex = 2
            End Try
        End Sub

    </script>

It’s more than annoying when you are are trying to identify each meta or link element in your html. Might be a bit much but I like to override the Render event to tidy this up myself.


    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim sw As New IO.StringWriter
        Dim localWriter As New HtmlTextWriter(sw)
        MyBase.Render(localWriter)
        Dim output As String = sw.ToString
        output = output.Replace(" /><title>", " />" & vbCrLf & vbTab & "<title>")
        output = output.Replace("</title><link", vbTab & "</title>" & vbCrLf & vbTab & "<link")
        output = output.Replace(" /><meta", " />" & vbCrLf & vbTab & "<meta")
        output = output.Replace(" /><link", " />" & vbCrLf & vbTab & "<link")
        output = output.Trim
        writer.Write(output)
    End Sub

Thats a starting point but it may require some experimentation to get it right.

  • edit - I should say that this is not the most efficient way to do things so don’t do it for performance critical web applications and there may well be better ways to do this which I’d be happy know about if someone could suggest them.

I find this annoying as well. Anytime .NET does work on a component like this, it turns it into a royal looking mess. I wish MS spent a little more time looking doing view source to see what they’re output actually looks like.

I don’t override the render event, but I do something similar whenever I can. I’ve defined a couple constants “tab” and “newLine” which I use when generating strings of XHTML in code. Each constant is defined by its Unicode encoding (e.g. \u0009). Not sure how much you will run into it, but as a front-end guy, my work uses a lot of highly custom XHTML. I’ve yet to find a decent framework in any technology, so when I have my pick, I work in C# for the architectural advantages, caching, garbage collection, etc. This has proved pretty handy for me.

I think there is a “view source formatted” extension for firefox that is what your boss really needs.

I’m wondering if it’s a newline thing. i.e Windows, Unix, Mac \r
.
. \

Are you working with your files on a Mac?

Hi, thanks for replying.

I work on a Windows 7 PC. I design in MS Expression Web 4.
And my sites are hosted on a GoDaddy Windows account.