Duplicate title tags in ASP.NET MVC 1

Quick “bug” I found in asp.net mvc.  When you create a new asp.net mvc application the default masterpage template has a  runat=”server” tag on the header like so:

<head runat=”server”>

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

<link href=”../../Content/Site.css” rel=”stylesheet” type=”text/css” />

</head>

This is fine except when you make a contentplaceholder to hold the title so each page could implement the title specific to that page like this:

<head runat=”server”>

<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<asp:ContentPlaceHolder ID=”HeaderContent” runat=”server” ></asp:ContentPlaceHolder>

<% Html.RenderPartial(“StaticContentHeader”); %>

</head>

When you use the contentplaceholder to add the title the runat=”server” tag will cause the asp.net engine to add an empty title tag to the head as if it doesn’t see that it was added from the MVC view causing the following output.

<head><meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<title>Test Page</title>

<link href=”/Content/MainCSS.css” rel=”stylesheet” type=”text/css” />

<script src=”/Scripts/MainJavascript.js” type=”text/javascript”></script><title>

</title></head>

The solution is to simply remove the runat tag.

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<asp:ContentPlaceHolder ID=”HeaderContent” runat=”server” ></asp:ContentPlaceHolder>

<% Html.RenderPartial(“StaticContentHeader”); %>

</head>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s