Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curious problem with the ContentPlaceHolder in the HTML Title tag of the masterpage (MVC2)

Ok, I'm sure it must be a silly mistake on my part, but I can't find where the problem is, and it's driving me nuts.

I have a master page, with this:

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

It's just the default HTML inserted by VS when I created the masterpage, I just added " - Company" at the end, so that I don't have to repeat that text in every single view.

On the views, I have, for example, this:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Some title for the view
</asp:Content>

As you can imagine, the final result is not what I expected. Instead of

<title>Some title for the View - Company</title>

I'm getting:

<title>Some title for the View</title>

Why?

like image 929
salgiza Avatar asked Aug 17 '10 12:08

salgiza


2 Answers

Seems to be a quirk about how Classic ASP.Net (aka WebForms) works. Phil Haacked on Title Tags and Master Pages is a great read.

Although he goes into depth about the reasons it works the way it does, it seems he isn't referring to MVC specifically. The first comment by Erik Porter has the crazy easy solution:

Change

<head runat="server">

to

<head>

Tada, fixed.

like image 117
Erik Philips Avatar answered Nov 16 '22 11:11

Erik Philips


Try this inside your title tag:

<asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= " - Company" %>

I ran into this a while back and putting the literal inside a code block cleared it up. I'm not really sure why, though, if someone has an explanation.

like image 36
Chris Avatar answered Nov 16 '22 11:11

Chris