如何在ASP.Net MVC 2的母版页上添加元标记

时间:2010-10-02 17:34:44

标签: asp.net-mvc asp.net-mvc-2

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

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

我现在意识到我必须放入meta标签,最好这样做:

<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>

OR

<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 个答案:

答案 0 :(得分:4)

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

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

然后在非主页面(如index.aspx)中你可以

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

在我看来,这将更容易控制元标记

答案 1 :(得分:1)

你不一定要用这些东西填满你的所有观点,这就是你有一个母版页的原因。 我会这样做:

Site.master中的

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

在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 %>' />