如果我們希望Title是動態產生的該怎麼辦?其實很簡單,只需要:
< title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>
< /title>
沒錯,就是這樣簡單明瞭,其實如果使用Razor View是不會有任何問題的。
Reference:http://stackoverflow.com/questions/6291131/how-to-set-the-global-text-of-title-in-site-master-page-using-asp-net-mvc-2-3
====[以下為舊資料,請勿參考]====
< title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
< asp:Literal ID="ltlTitleBack" runat="server" Text='<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
< /title>
是行不通的,它會顯示< asp:Literal不能包含子控制項,不過我找到一個可行的解決方案,但不知道是不是最好的:http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
需要將Site.Master的程式碼改為:
< title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
< asp:Literal ID="ltlTitleBack" runat="server" Text='<%$ Code: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
< /title>
然後某個Class中加入
using System.Web.Compilation;
using System.CodeDom;
using System.Web.UI;
//修正Literal
[System.Web.Compilation.ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
object parsedData, ExpressionBuilderContext context)
{
return new CodeSnippetExpression(entry.Expression);
}
}
最後在Web.config中的