views:

283

answers:

4

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):

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

A: 

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.

Joel Coehoorn
A: 

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;
Dan Diplo
Please note the last sentence of my question: I am using MVC so code-behind is not an acceptable option.
Nathan Taylor
Sorry, missed that part - Ignore.
Dan Diplo
A: 

This is a possibility -

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

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

dagray
+1  A: 

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>
John Hartsock
I like this solution, although I can't apply it in the top down approach I was looking for. Realistically this change needs to be made to about 10 sites so I'd like it to require as little contact with the content pages as possible.
Nathan Taylor
Nathan look at my modifications, The second method would allow you to simply run through your controller actions and add ViewData["Title"] = "something" to each action that returns a View. You could do a search and replace for this
John Hartsock
I like the second approach. Nice. Failing something better later today, I'll accept it. :)
Nathan Taylor