Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in ASP.NET MVC, is there a ContentPlaceHolder for the title?

Tags:

asp.net-mvc

In a new ASP.NET site there is a ContentPlaceHolder for the title:

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

In the page:

<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    About Us
</asp:Content>

Why is this? Why can't the title property/attribute be used on the page directive?

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Both methods have the same result. For me the ContentPlaceHolder approach seems hackish.

If you needed a dynamic title you can do it like this in the aspx page:

<%= this.Title = "About Me" %>
like image 805
Chuck Conway Avatar asked Jun 29 '09 18:06

Chuck Conway


2 Answers

See this post.

like image 70
mxmissile Avatar answered Sep 27 '22 17:09

mxmissile


If you used Title attribute of @Page directive instead you would have to set an application-wide title in each view duplicating code and violating single responsibility principle.

like image 30
Alexander Prokofyev Avatar answered Sep 27 '22 18:09

Alexander Prokofyev