开发者

mvc - page title - two different settings

开发者 https://www.devze.com 2023-01-24 06:44 出处:网络
Here is my code in my content page and master page respectively: <asp:Content ID=\"Content1\" ContentPlaceHolderID=\"TitleContent\" runat=\"server\">

Here is my code in my content page and master page respectively:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
     <%=Model.Title %> 
</asp:Content>

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

this works for me except sometimes content Pages don'开发者_如何学Ct have titles. So the page title ends up being "-Website" instead of "Website".

Should I just replace the above by this or is there a better way?

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        - <%=Model.Title %> //downside: remember to append "dash" inside every single view. 
    </asp:Content>

    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /> WebsiteName</title>


The first version is close, just add a little logic to check if there is a subtitle:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
     <%=!string.IsNullOrEmpty(Model.Title) ? Model.Title + " - " : ""  %> 
</asp:Content>

<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />WebsiteName</title>


A smarter content placeholder

For what I know it would probably be the best way to write a custom web control that inherits from System.Web.UI.WebControls.ContentPlaceHolder class. To make it as universal as possible (so you can publish it on the web) you could add three additional properties:

string Prefix { get; set; }
string Suffix { get; set; }
bool AddOnEmpty { get; set; }

This will make it quite usable. You would be able to put content inside parentheses, add dashes, semicolons etc. The boolean property defines whether prefix and suffix should be added even if there's no content.

In your case this control on master page pages would look like:

<title>
    Web site name
    <asp:TitlePlaceHolder ID="TitleContent" runat="server"
         Prefix="- "
         AddOnEmpty="false" />
</title>

but you could easily make things like:

<title>
    Web site name
    <asp:TitlePlaceHolder ID="TitleContent" runat="server"
         Prefix="["
         Suffix="]"
         AddOnEmpty="false" />
</title>

or similar.

0

精彩评论

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