开发者

How to add meta tags on a master page for ASP.Net MVC 2

I have a master page currently with the below for the title:

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

I've now realised I have to put meta tags in, would that be best done like so:

<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:ContentPla开发者_开发技巧ceHolder ID="keysContent" runat="server" />"


Yes, you could also add page specific meta tags by adding another ContentPlaceHolder for meta tags:

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

Then in your non master-page (like index.aspx) you could just

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

This would be much easier, in my opinion, to control the meta tags


you don't necessarily have to fill all your views with this stuff, that's why you have a master page. I would do like this:

in Site.master:

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

in 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 %>' />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜