MVC3 Meta tag Dynamically change

2019-05-30 14:33发布

问题:

I am working on MVC3, I am using Razor engine i have a layout.cshtml page which contains meta tag like

<meta property="og:url" content="http://www.mywebsite.com/" />

I have some content pages like abc.cshtml which has Layout.cshtml page as there's layout,so they are getting meta tags from layout.cshtml,i want that abc.cshtml should have its own meta tag like

<meta property="og:url" content="http://www.mywebsite.com/controller/abc" />

How can i do this? OR can i change meta tag dynamically??????

回答1:

Yes you can pass that value from each or view action in ViewBag like below...

ViewBag.OgURL = "http://www.mywebsite.com/controller/abc";

then in you layout.cshtml

@ViewBag.OgURL

or set above head tag in you layout.cshtml..

@RenderSection("head", false)

and add in your view...

@section head {
<meta property="og:url" content="http://www.mywebsite.com/controller/abc" />
}