1
votes

How to use meta tags on single pages using asp.net webpages. I'm currently using a layout master page with default meta tags which works across several pages, but I can't seem to find a way to override the meta tags for sub pages of the master file.

On the sub pages I've the following but it does work.

@{
    Layout = "~/siteLayout.cshtml";
    Page.Title = "Meta Page";
    Page.MetaKeywords = "keyword1, keyword2, keyword3";
}

Can anyone please assist or point me in the right direction, thanks you!

1

1 Answers

0
votes

Use in your layout something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>@ViewBag.Title</title>
    <meta name="keywords" content="@ViewBag.Keywords">

And then in your controller, layout or view, you have to set ViewBag.Title and ViewBag.Keywords. Here is an example of how to set it in a view that uses layout above.

@{
   Layout = "~/Views/Shared/_YourLayout.cshtml";
   ViewBag.Title = "My Page Title";
   ViewBag.Keywords = "keyword1, keyword2";
}

The ViewBag property enables you to dynamically share values from the controller to the view. Internally ViewBag properties are stored as name/value pairs in the ViewData that is like a dictionary where you can store random stuff that you want to use in your view.