I am using ASP.NET MVC 2 & 3 with aspx View not Razor View, my question is:
How to set the title in Site.Master page? I wish every pages which use Site.Master page can add a Master title follow the page title show like: “Index -MasterTitle” ; “About -MasterTitle”.
I’ve try in Site.Master page and it’s doesn’t work:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
-MasterTitle
</title>
so I try to use asp:Literal server control:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server">-MasterTiltle</asp:Literal>
</title>
Or:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server" Text="-MasterTiltle"></asp:Literal>
</title>
fine, it’s solve the problem, but later I want to load the value of MasterTitle from web.config, I’ve try:
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server">
<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>
</asp:Literal>
</asp:Literal>
compiler toled me a server control can’t contain a child control, so I try the other:
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal ID="ltlTitleBack" runat="server" Text='<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
</asp:Literal>
</title>
it’s compiler ok but not the answer what I want cause it show: “pagetilte <%: System.Web.Configuration.WebConfigurationManager.AppSettings[“SiteTile”] %>“
and later I found a way can solve this problem here: CodeExpressionBuilder
but I think that is not a proper solution cause I should set too many things that not relate to ASP MVC. Is there any better solution can solve this problem?
Note:
- I don’t want set the value of MasterTitle in every pages not Site.Master page, that’s a stupid way.
- I don’t want set the value of MasterTitle in Controller or Action or Model, that’s not right.
- I don’t want set the value of MasterTitle via ViewData.
- I don’t want to use CodeExpressionBuilder.
- I’m not use Razor View.
thanks for any help.
Edit: Actually I want something like web.config so I can change the value of MasterTitle when server is running. 🙂
I had similar issues and here is what I had to use: