开发者

A different title in one page in ASP.NET MVC without creating another master page

开发者 https://www.devze.com 2022-12-15 19:44 出处:网络
In my master page I define the title of the page as follows: <title开发者_JAVA百科><asp:ContentPlaceHolder ID=\"TitleContent\" runat=\"server\" /><%= \"- MyWebSite\". %></title&g

In my master page I define the title of the page as follows:

<title开发者_JAVA百科><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.


You could put the - MyWebSite inside another ContentPlaceHolder, and make a Content control for it in the home page (only).

In the master page, you put something like

<title>
  <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
  <asp:ContentPlaceHolder ID="TitleSuffixContent" runat="server">
    - MyWebSite
  </asp:ContentPlaceHolder>
</title>

and while your products page remain

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
  Products
</asp:Content>

your home page would be something like:

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
  MyWebSite
</asp:Content>
<asp:Content ID="TitleSuffix" ContentPlaceHolderID="TitleSuffixContent" runat="server">
</asp:Content>

or even:

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
  MyWebSite
</asp:Content>
<asp:Content ID="TitleSuffix" ContentPlaceHolderID="TitleSuffixContent" runat="server">
  - Your place on the interwebs
</asp:Content>


what you need to do it's simply remove runat="server" attribute in your head tag

0

精彩评论

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