标题元素中的ASP.NET换行符

时间:2023-01-14 16:33:13

I'm trying to optimize SEO readability on our websites and one issue I've come across is ASP.NET butchering the title element of my MasterPage. Entered as such in my MasterPage (manually reformatted to remove line breaks caused by the <% %> tags):

我正在尝试优化我们网站上的SEO可读性,我遇到的一个问题是ASP.NET屠宰我的MasterPage的标题元素。在我的MasterPage中输入(手动重新格式化以删除由<%%>标记引起的换行符):

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - <%=WebsiteSettings.WebsiteName %></title>

This is the output I receive:

这是我收到的输出:

   <title>
    Home
 - Website Name</title>

As you can see ASP.NET is adding preceding and trailing line breaks where the <asp:ContentPlaceHolder /> is substitute becaused Visual Studio auto-formats <asp:Content /> to start and end with a line break. Obviously, this can be prevented in the Visual Studio formatting options, but this is not ideal because I only would want to remove that behavior for the TitleContent placeholder and not the rest.

正如您所看到的,ASP.NET正在添加前缀和尾随换行符,其中 被替换为Visual Studio自动格式化 以开始和结束换行符。显然,这可以在Visual Studio格式化选项中防止,但这并不理想,因为我只想删除TitleContent占位符而不是其余的行为。

Is there any way I can ensure my Title is trimmed before it is rendered? I am using MVC so code-behind is not an acceptable option.

有什么方法可以确保我的标题在渲染之前被修剪了吗?我正在使用MVC,因此代码隐藏不是一个可接受的选择。

5 个解决方案

#1


2  

The following should allow you to keep from copying and pasting code.

以下应允许您不要复制和粘贴代码。

Option 1

选项1

Since your using MVC create a HTML Helper Like this:

由于您使用MVC创建HTML Helper,如下所示:

namespace [ProjectName].Web.Views
{
    public static class HtmlHelpers        
    {
            public static MvcHtmlString GetFullPageTitle(this HtmlHelper helper, string PageTitle)
            {
                return MvcHtmlString.Create(PageTitle + " - " + WebsiteSettings.WebsiteName)
            }
    }
}

Now in your Master Page just put this

现在在你的母版页中就是这样

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

Then in your pages use this

然后在你的页面中使用它

<asp:Content ID="PageTitleContent" ContentPlaceHolderID="TitleConent" runat="server">
  <%=Html.GetFullPageTitle("Some PageTitle")%>
</asp:Content>

Option 2

选项2

Note: if you populate Data in your Action then you dont have to Add this to ever page.

注意:如果您在Action中填充数据,那么您不必将此添加到任何页面。

Like so:

像这样:

public ActionResult myAction()
{
     ViewData["Title"] = "MyActionTitle";
     return View()
}

Then in your Master page you would simply do the following

然后在您的母版页中,您只需执行以下操作即可

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= ViewData["Title"] + "-" +  WebsiteSettings.WebsiteName %></asp:ContentPlaceHolder></title>

The nice thing about this is if you wanted to you could override what the title says in each page by doing this

关于这一点的好处是如果你想通过这样做可以覆盖每个页面中标题所说的内容

<asp:Content ID="PageTitleContent" ContentPlaceHolderID="TitleConent" runat="server">
       My Override Title
    </asp:Content>

#2


1  

If you are really bothered (and I don't see why you would be given whitespace is not important in HTML) you could try setting it in code-behind something like this:

如果你真的很烦(我不明白你为什么会在HTML中给出空格并不重要)你可以尝试在代码隐藏中设置这样的东西:

Page.Title = WebsiteSettings.WebsiteName + " " + Page.Title;

#3


1  

Using regular expressions, as dagray said, is the safest and easiest approach.

正如dagray所说,使用正则表达式是最安全和最简单的方法。

This code replaces only the first occurrence of newline/characters in first title tag.

此代码仅替换第一个标题标记中第一次出现的换行符/字符。

void TrimTitleRegex(ref string content)
{
    System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"\<title\>(.*?)\<\/title\>");
    var result = rgx.Replace(content,
    m =>
    {
        var codeString = m.Groups[1].Value;
        // then you have to evaluate this string
        codeString = System.Text.RegularExpressions.Regex.Replace(codeString, @"\r\n?|\n", "");
        codeString = String.Format("<title>{0}</title>", codeString);
        return codeString.Trim();
    }, 1);

    content = result;
}

#4


0  

You could try a literal control, though I suspect that won't work in the document header outside the asp.net form. You could also try setting the title via code-behind.

您可以尝试文字控件,但我怀疑它不会在asp.net表单之外的文档头中工作。您也可以尝试通过代码隐藏设置标题。

#5


0  

This is a possibility -

这是一种可能性 -

Override the rendering routine to remove whitespace with regexp's:

覆盖渲染例程以删除带有正则表达式的空格:

http://madskristensen.net/post/Remove-whitespace-from-your-pages.aspx

http://madskristensen.net/post/Remove-whitespace-from-your-pages.aspx

#1


2  

The following should allow you to keep from copying and pasting code.

以下应允许您不要复制和粘贴代码。

Option 1

选项1

Since your using MVC create a HTML Helper Like this:

由于您使用MVC创建HTML Helper,如下所示:

namespace [ProjectName].Web.Views
{
    public static class HtmlHelpers        
    {
            public static MvcHtmlString GetFullPageTitle(this HtmlHelper helper, string PageTitle)
            {
                return MvcHtmlString.Create(PageTitle + " - " + WebsiteSettings.WebsiteName)
            }
    }
}

Now in your Master Page just put this

现在在你的母版页中就是这样

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

Then in your pages use this

然后在你的页面中使用它

<asp:Content ID="PageTitleContent" ContentPlaceHolderID="TitleConent" runat="server">
  <%=Html.GetFullPageTitle("Some PageTitle")%>
</asp:Content>

Option 2

选项2

Note: if you populate Data in your Action then you dont have to Add this to ever page.

注意:如果您在Action中填充数据,那么您不必将此添加到任何页面。

Like so:

像这样:

public ActionResult myAction()
{
     ViewData["Title"] = "MyActionTitle";
     return View()
}

Then in your Master page you would simply do the following

然后在您的母版页中,您只需执行以下操作即可

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= ViewData["Title"] + "-" +  WebsiteSettings.WebsiteName %></asp:ContentPlaceHolder></title>

The nice thing about this is if you wanted to you could override what the title says in each page by doing this

关于这一点的好处是如果你想通过这样做可以覆盖每个页面中标题所说的内容

<asp:Content ID="PageTitleContent" ContentPlaceHolderID="TitleConent" runat="server">
       My Override Title
    </asp:Content>

#2


1  

If you are really bothered (and I don't see why you would be given whitespace is not important in HTML) you could try setting it in code-behind something like this:

如果你真的很烦(我不明白你为什么会在HTML中给出空格并不重要)你可以尝试在代码隐藏中设置这样的东西:

Page.Title = WebsiteSettings.WebsiteName + " " + Page.Title;

#3


1  

Using regular expressions, as dagray said, is the safest and easiest approach.

正如dagray所说,使用正则表达式是最安全和最简单的方法。

This code replaces only the first occurrence of newline/characters in first title tag.

此代码仅替换第一个标题标记中第一次出现的换行符/字符。

void TrimTitleRegex(ref string content)
{
    System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"\<title\>(.*?)\<\/title\>");
    var result = rgx.Replace(content,
    m =>
    {
        var codeString = m.Groups[1].Value;
        // then you have to evaluate this string
        codeString = System.Text.RegularExpressions.Regex.Replace(codeString, @"\r\n?|\n", "");
        codeString = String.Format("<title>{0}</title>", codeString);
        return codeString.Trim();
    }, 1);

    content = result;
}

#4


0  

You could try a literal control, though I suspect that won't work in the document header outside the asp.net form. You could also try setting the title via code-behind.

您可以尝试文字控件,但我怀疑它不会在asp.net表单之外的文档头中工作。您也可以尝试通过代码隐藏设置标题。

#5


0  

This is a possibility -

这是一种可能性 -

Override the rendering routine to remove whitespace with regexp's:

覆盖渲染例程以删除带有正则表达式的空格:

http://madskristensen.net/post/Remove-whitespace-from-your-pages.aspx

http://madskristensen.net/post/Remove-whitespace-from-your-pages.aspx