I currently have a master layout page with a default开发者_开发问答 title, and meta content. How can I override the title and meta content in child pages?
I found a way that works for now. I'm wure it can be improved, but this is the closet thing to an ideal solution that I have come across.
In the controllers assign whatever meta info you need for that view.
ViewBag.Title = "some title";
ViewBag.MetaDescription = "some description";
etc...
and in the master layout page I do the following
<title>@ViewBag.Title</title>
@if(ViewBag.MetaDescription != null)
{
<meta name="description" content="@ViewBag.MetaDescription" />
}
@if(ViewBag.MetaKeywords != null)
{
<meta name="keywords" content="@ViewBag.MetaKeywords" />
}
define a ContentPlaceHolder (@RenderSection in Razor) and fill it in the views that use the master page, using Content (@section in Razor).
精彩评论