Reputation: 53
I am looking for a function to add meta title and description when I enter and call values in view at .net core. I have tried the following code but it did not.How do I include HtmlMeta and Page? Does this code work for me? I am waiting for your help.
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Your Page Title";
HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = "description";
metaDescription.Content = "Your Page Description";
Page.Header.Controls.Add(metaDescription);
HtmlMeta metaKeywords = new HtmlMeta();
metaKeywords.Name = "keywords";
metaKeywords.Content = "Your Page Keywords";
Page.Header.Controls.Add(metaKeywords);
}
Upvotes: 0
Views: 1540
Reputation: 5719
In the layout page add new section inside <head>
tags:
<head>
...
@await RenderSectionAsync("Header", required: false)
</head>
Then you can add meta tags to the header section from any view:
@section Header {
<meta name="description" content="@Model.Description"/>
<meta name="keywords" content="Model.KeyWords"/>
}
In your page model you can provide the values for Description
and KeyWords
then pass it to the view.
Upvotes: 3