删除ASP.NET HTML页面标题中的额外空格?

我有个问题.

我使用VB.NET将页面的标题值指定为page.title =“a”但是当我运行页面并查看页面查看源时,我发现它显示为< title>一个< / title>

问题是我要删除标题标记之间的所有空格,它显示如下< title> a< / title>

提前致谢!

最佳答案 据我所知,这只是一个带有ASP.NET渲染的怪癖(bug?).

我自己偶然发现了这个,并在这里找到了这个修复:Weird white space in title tag.如果它困扰你,那么只需将它粘在你的页面代码中即可修复它:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Dim stringWriter As New System.IO.StringWriter()
    Dim htmlWriter As New HtmlTextWriter(stringWriter)
    MyBase.Render(htmlWriter)
    Dim html As String = stringWriter.ToString()
    Dim t1 As Integer = html.IndexOf("<title>")
    Dim t2 As Integer = html.IndexOf("</title>") + 8
    Dim newTitleTag As String = html.Substring(t1, t2 - t1)
    html = html.Replace(newTitleTag, String.Format("<title>{0}</title>", Me.Title))

    writer.Write(html)

End Sub
点赞