Add Meta tag in View page using MVC-5

Jatin Gadhiya picture Jatin Gadhiya · Apr 21, 2014 · Viewed 32.6k times · Source

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.

Answer

meziantou picture meziantou · Apr 21, 2014

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>