如何使用 ASP.NET MVC 2 & 在 Site.Master 页面中设置标题的全局文本3?
我正在使用 ASP.NET MVC 2 & 3 使用aspx视图而不是Razor视图,我的问题是:
如何在Site.Master页面中设置标题?我希望每个使用 Site.Master 页面的页面都可以在页面标题显示后添加主标题,例如:“Index -MasterTitle”; “关于-MasterTitle”。
我在 Site.Master 页面中尝试过,但它不起作用:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
-MasterTitle
</title>
所以我尝试使用 asp:Literal 服务器控件:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server">-MasterTiltle</asp:Literal>
</title>
或者:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server" Text="-MasterTiltle"></asp:Literal>
</title>
很好,它解决了问题,但后来我想从 web.config 加载 MasterTitle 的值,我尝试过:
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal runat="server">
<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>
</asp:Literal>
</asp:Literal>
编译器告诉我服务器控件不能包含子控件,所以我尝试另一个:
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<asp:Literal ID="ltlTitleBack" runat="server" Text='<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
</asp:Literal>
</title>
编译器可以,但不是我想要的答案,因为它显示:“pagetilte <%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>"
后来我找到了一种方法可以在这里解决这个问题: CodeExpressionBuilder
但我认为这不是一个正确的解决方案,因为我应该设置太多与 ASP MVC 无关的东西。有没有更好的方案可以解决这个问题呢?
注意:
- 我不想在每个页面而不是 Site.Master 页面中设置 MasterTitle 的值,这是一种愚蠢的方法。
- 我不想在 Controller 或 Action 或 Model 中设置 MasterTitle 的值,这是不对的。
- 我不想通过 ViewData 设置 MasterTitle 的值。
- 我不想使用 CodeExpressionBuilder。
- 我不使用 Razor View。
感谢您的帮助。
编辑:实际上我想要像 web.config 这样的东西,这样我就可以在服务器运行时更改 MasterTitle 的值。 :)
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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,这是我必须使用的:
I had similar issues and here is what I had to use:
您应该将资源文件用于此类文本...
http://msdn .microsoft.com/en-us/library/ms227427.aspx
它不仅可以让您将文本设置在一个不错的中心位置,而且它就是为此目的而设计的 - 它还将使您更容易打造区域性的稍后为您的网站提供语言版本!
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!