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

删除回忆录丶 提交于 2020-01-04 01:53:07

问题


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:ContentPlaceHolder ID="keysContent" runat="server" />"

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/3846810/how-to-add-meta-tags-on-a-master-page-for-asp-net-mvc-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!