开发者

How to modify <head> elements programmatically?

Is there a way t开发者_开发百科o programmatically access and modify the <head> section of the page in ASP.NET MVC? I need to update the page's <meta> tags depending on which data the user is viewing on any given page.


You could use a content placeholder in the master page which you override in each view:

<head>
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <asp:ContentPlaceHolder ID="Metas" runat="server" />
...

and in the view:

<asp:Content ID="IndexMetas" ContentPlaceHolderID="Metas" runat="server">
    <meta name="keywords" content="some keywords specific to the view" />
</asp:Content>


Try:

<meta name="description" content="<%: Model.Meta %>" />


In ASP.NET 4.0 there are several new properties of the Page which you can use to set meta-tags directly like this:

Page.MetaKeywords = "asp.net,c#"; 
Page.MetaDescription = "This is my stackoverflow post";

You can read more about them here http://weblogs.asp.net/dotnetstories/archive/2010/03/23/asp-net-4-0-meta-tags-and-search-engine-optimisation.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜