开发者

How to set the global text of title in Site.Master page using ASP.NET MVC 2 & 3?

开发者 https://www.devze.com 2023-03-11 07:03 出处:网络
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 t

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开发者_如何学C cause I should set too many things that not relate to ASP MVC. Is there any better solution can solve this problem?

Note:

  1. I don't want set the value of MasterTitle in every pages not Site.Master page, that's a stupid way.
  2. I don't want set the value of MasterTitle in Controller or Action or Model, that's not right.
  3. I don't want set the value of MasterTitle via ViewData.
  4. I don't want to use CodeExpressionBuilder.
  5. 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:

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


You should use your Resource files for this kind of text...

http://msdn.microsoft.com/en-us/library/ms227427.aspx

Not only does it let you set the text in a nice central place, but it is designed for this exact purpose - it will also make it easier for your to create regional language versions of your website later on!

0

精彩评论

暂无评论...
验证码 换一张
取 消