开发者

What's a good way to do meta tags in asp.net mvc 3?

开发者 https://www.devze.com 2023-03-22 19:47 出处:网络
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

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消