How to add meta tags on the main page for ASP.Net MVC 2

I currently have a homepage with the name below:

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

Now I realized that I need to add meta tags, is it better to do this:

<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" />"
+3
source share
2 answers

Yes, you can also add meta tags for specific pages by adding another ContentPlaceHolder for meta tags:

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

Then on your non-master page (e.g. index.aspx) you could just

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

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

+4
source

you don’t have to fill all your views with this material, so you have a home page. I would do the following:

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 %>' />
+1
source

Source: https://habr.com/ru/post/1767679/


All Articles