开发者

ASP.NET Line Breaks in Title Element

I'm trying to 开发者_如何学Pythonoptimize 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):

<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.

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.


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

Option 1

Since your using MVC create a HTML Helper Like this:

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

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

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>


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:

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


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

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;
}


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.


This is a possibility -

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜