Why does asp: ContentPlaceHolder in the title replace the whole title?

Using ASP.Net MVC on my Site.Master I have:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - MySite</title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

then on each view I have something like:

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
    Home
</asp:Content>

and I was expecting the result:

 <title>Home - MySite</title>

but instead I have:

 <title>Home</title>

Any ideas why?

+3
source share
5 answers

Use this:

<title>
  <asp:ContentPlaceHolder ID="titleContent" runat="server" />
  <%= "- My Site" %>
</title>

The reason is that everything displayed in the head is displayed as a control.

See question for some additional links and other ways to solve it.

+7
source

You may have a Title attribute in the <% @ Page%> directive.

+1
source

wired ,

   <title><%= Html.Encode(ViewData["Title"])  %> - mysite</title> 

0

Have you tried to take a bit runat="server"from the header tag? I don't have a testing machine right now, but it looks a little weird to me.

0
source

I use this markup in the Site.Master file:

<title>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    <asp:Literal runat="server" Text=" - MySite" />
</title>

It looks like a boymc suggestion.

0
source

Source: https://habr.com/ru/post/1711040/


All Articles