代码之家  ›  专栏  ›  技术社区  ›  Jon

如何在母版页上添加元标记ASP.NetMVC 2型

  •  3
  • Jon  · 技术社区  · 15 年前

    我有一个母版页,标题如下:

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

    <asp:ContentPlaceHolder ID="TitleContent" runat="server">
    <title>Title</title>
    <meta name="Description" content=" ... add description here ... "> 
    <meta name="Keywords" content=" ... add keywords here ... ">
    </asp:ContentPlaceHolder>
    

    或者

    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <meta name="Description" content="<asp:ContentPlaceHolder ID="descContent" runat="server" />"> 
    <meta name="Keywords" content="<asp:ContentPlaceHolder ID="keysContent" runat="server" />"
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   Buildstarted    15 年前

    是的,您还可以通过为meta标记添加另一个ContentPlaceHolder来添加页面特定的meta标记:

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

    然后在你的非母版页(比如索引.aspx)你可以

    <asp:Content id="MetaTags" ContentPlaceHolderID="MetaTagsContent" runat="server">
        <meta name="Description" content="your content" />
    </asp:Content>
    

    在我看来,控制meta标记要容易得多

        2
  •  1
  •   Omu    15 年前

    我想这样做:

    在站点.master:

    <% Html.RenderPartial("meta"); %>
    

    在元数据.ascx

        <%
        string controller = ViewContext.RouteData.Values["Controller"];
        string action = ViewContext.RouteData.Values["Action"];
        string content = "default description";
        if(controller == "Home") content = "home specific";
        //or like this
        if(controller == "Home" && action == "Index") content = "bla bla";
    //this way you can put the same description for a specific group, you decide
        %>
        <meta name="Description" content='<%=content %>' />