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.
The following should allow you to keep from copying and pasting code.
Option 1
Since your using MVC create a HTML Helper Like this:
Now in your Master Page just put this
Then in your pages use this
Option 2
Note: if you populate Data in your Action then you dont have to Add this to ever page.
Like so:
Then in your Master page you would simply do the following
The nice thing about this is if you wanted to you could override what the title says in each page by doing this