ASP.NET MVC 中的一个页面中的不同标题,无需创建另一个母版页
在我的母版页中,我定义页面标题如下:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= "- MyWebSite". %></title>
然后在每个视图上我都有类似的内容:
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Products
</asp:Content>
这样我总是以标题中的网站名称结束,例如“产品 - MyWebSite”。
但对于一个且只有一个页面(主页),我只需要网站的名称,如“MyWebSite”。理想情况下,我希望对于每个未定义标题的视图,我只显示网站的名称。问题是,在我当前的代码中,我最终以“- MyWebSite”作为标题。
有什么想法如何在没有大量重复的情况下做到这一点?绝对不会创建另一个模板。
In my master page I define the title of the page as follows:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= "- MyWebSite". %></title>
and then on every view I have something like:
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Products
</asp:Content>
so that I always end up with the name of the website in the title, like "Products - MyWebSite".
But for one and only one page, the home page, I want only the name of the site, as in "MyWebSite". Ideally I would like that for every view that doesn't define a title, I just show the name of the site. The problems is that with my current code I end up with "- MyWebSite" as title.
Any ideas how to do this without a lot of duplication? Definitely not creating another template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
- MyWebSite
放入另一个ContentPlaceHolder
中,并在主页中为其创建一个Content
控件(仅限)。在母版页中,您可以放置类似的内容
,而当您的产品页面保留在
主页上时,您的主页将类似于:
甚至:
You could put the
- MyWebSite
inside anotherContentPlaceHolder
, and make aContent
control for it in the home page (only).In the master page, you put something like
and while your products page remain
your home page would be something like:
or even:
您需要做的只是删除 head 标记中的
runat="server"
属性what you need to do it's simply remove
runat="server"
attribute in your head tag