Add Meta tag in View page using MVC-5
Asked Answered
A

4

21

I have two meta tag in _Layout.cshtml master page and now i want to add meta tags in someone.cshtml view page.

and i also try with this code

put in _layout.cshtml master page @RenderSection("metatags",false);

put in someone.cshtml like @section metatags { <meta ... /> }

but not get success.

and also try with add meta tag jquery but not worked fine.

Antilogarithm answered 21/4, 2014 at 11:35 Comment(2)
Here it is c-sharpcorner.com/UploadFile/abhikumarvatsa/…Maguire
@RenderSection and @section work for me.Instigate
A
16

I found a way that works for now.

This solution mostly use when multiple master page.

In the controllers / action assign whatever meta info you need for that view.

ViewBag.Title = "some title";
ViewBag.MetaDescription = "some description";

In the View or master page get whatever meta info you need for that view.

 @if(ViewBag.MetaDescription != null)
    {
        <meta name="description" content="@ViewBag.MetaDescription" />
    }

    @if(ViewBag.MetaKeywords != null)
    {
        <meta name="keywords" content="@ViewBag.MetaKeywords" />
    }
Antilogarithm answered 22/4, 2014 at 6:12 Comment(0)
C
59

It should work.

Here's my master page _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    @RenderSection("metatags", false)
    <title>My ASP.NET Application</title>
</head>
<body>
    @RenderBody()
</body>
</html>

Here's the index.cshtml file

@section metatags
{
    <meta name="test" content="test"/>
    <meta name="test2" content="test"/>
}

<div>Page content</div>

The result

<html>
<head>
    <meta charset="utf-8">

    <meta name="test" content="test">
    <meta name="test2" content="test">

    <title>My ASP.NET Application</title>
</head>
<body>        

<div>Page content</div>    

</body>
</html>
Canard answered 21/4, 2014 at 11:56 Comment(8)
yes i have use this code but not working. see i used in _layout.cshtml { <meta name="description" content="@(Html.NopMetaDescription())" /> <meta name="keywords" content="@(Html.NopMetaKeywords())" /> @RenderSection("metatags",false); } and use in someone.cshtml like @section metatags { <meta ... /> }Antilogarithm
Try to replace @RenderSection("metatags",false) by @RenderSection("metatags", true) to ensure the section is used.Canard
Error : Section not defined: "metatags". in _Layout.cshtmlAntilogarithm
Do you have more than one master page?Canard
yes.. _Root.cshtml and in this master page included _Root.Head.cshtml and and i am add this { @RenderSection("metatags",required : true);} in _Root.Head.cshtml because in this page already added head tag and also scripts and cssAntilogarithm
RenderSection can only be used in Layout file (e.g. MasterPage), not partial files. #7808877Canard
so how to do now in this situation because i have a multiple master page.. give me any solution. pleaseAntilogarithm
let us continue this discussion in chatAntilogarithm
A
16

I found a way that works for now.

This solution mostly use when multiple master page.

In the controllers / action assign whatever meta info you need for that view.

ViewBag.Title = "some title";
ViewBag.MetaDescription = "some description";

In the View or master page get whatever meta info you need for that view.

 @if(ViewBag.MetaDescription != null)
    {
        <meta name="description" content="@ViewBag.MetaDescription" />
    }

    @if(ViewBag.MetaKeywords != null)
    {
        <meta name="keywords" content="@ViewBag.MetaKeywords" />
    }
Antilogarithm answered 22/4, 2014 at 6:12 Comment(0)
P
1

If are using nested layouts you'll need to follow this guideline:

@RenderSection in nested razor templates

The technique you're using should work, if it is not, maybe that's the reason.

But looking at the intended use in your own answer, if you only need to modify the keywords and description tags there are apis in NopCommrece for that.

In your master layout:

<meta name="description" content="@(Html.NopMetaDescription())" />
<meta name="keywords" content="@(Html.NopMetaKeywords())" />

and in the client cshtml files

@{
    Html.AddMetaDescriptionParts(Model.MetaDescription);
    Html.AddMetaKeywordParts(Model.MetaKeywords);
}

There are plenty of samples for this in the NopCommerce code.

Propraetor answered 22/4, 2014 at 11:46 Comment(0)
L
0

here is a good sample on how to dynamically creating meta tags in asp.net mvc:

public string HomeMetaTags()
{
    var strMetaTag = new StringBuilder();
    strMetaTag.AppendFormat(@"<meta content='{0}' name='Keywords'/>","Home Action Keyword");
    strMetaTag.AppendFormat(@"<meta content='{0}' name='Descption'/>", "Home Description Keyword");
    return strMetaTag.ToString();
}

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    ViewBag.MetaTag = HomeMetaTags(); 
    return View();
}


<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
   @Html.Raw(ViewBag.MetaTag)
</head>
Language answered 18/6, 2015 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.