在 C# 中以编程方式添加元标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1432431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:28:26  来源:igfitidea点击:

Adding meta tag programmatically in C#

c#asp.net

提问by subha

I'm trying to programmatically add a <meta>. It is working fine when there is a Headelement with runat = "server"in the .aspxpage.

我正在尝试以编程方式添加一个<meta>. 它工作正常时,有一个Head元素与runat = "server".aspx页。

The code behind is:

后面的代码是:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Header.Controls.Add(meta);

But I have some script in the head tag which contains code blocks like <% ... %>, so I cannot keep the runat = "server"value.

但是我在 head 标签中有一些脚本,其中包含像 一样的代码块<% ... %>,所以我无法保留该runat = "server"值。

The problem is I have to add the meta tag programmatically, because it depends on a value from the database.

问题是我必须以编程方式添加元标记,因为它取决于数据库中的值。

Is there a way to solve this issue so that my script inside the head element works as usual and I can add a meta tag programmatically?

有没有办法解决这个问题,以便我在 head 元素内的脚本照常工作,并且我可以以编程方式添加元标记?

采纳答案by awe

OK, I tested the answer by veggerby, and it works perfectly:

好的,我测试了veggerby答案,它完美地工作:

In the <header>section:

在该<header>部分:

<asp:PlaceHolder id="MetaPlaceHolder" runat="server" />

Note that Visual Studio might show a warning on the PlaceHolder tag, because it is not recognised as a known element inside the header, but you can ignore this. It works.

请注意,Visual Studio 可能会在 PlaceHolder 标记上显示警告,因为它未被识别为标头内的已知元素,但您可以忽略这一点。有用。

In the C# code:

在 C# 代码中:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
MetaPlaceHolder.Controls.Add(meta);

Alternatively (since you already have code blocks using <% %>in your header section), you can tag the meta directly and retrieve only the value from server side:

或者(因为您已经<% %>在标题部分使用了代码块),您可以直接标记元并仅从服务器端检索值:

<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />

回答by Mike

Try moving whatever it is that you are doing in the <% .... %> to the code-behind. If you are using the script to add content into the page, you can replace it with an asp:Literal control and then set the value you were previously calculating in the script block to the code-behind and set Literal.Text to that value.

尝试将您在 <% .... %> 中所做的任何事情移到代码隐藏中。如果您使用脚本向页面添加内容,您可以将其替换为 asp:Literal 控件,然后将您之前在脚本块中计算的值设置为代码隐藏,并将 Literal.Text 设置为该值。

回答by veggerby

I haven't tested it, but maybe you can add an <asp:Placeholder>inside the <head></head>tag and add the meta tags to this.

我还没有测试过,但也许您可以<asp:Placeholder><head></head>标签内添加一个并将元标签添加到其中。

回答by Timothy Khouri

Or you could just put your meta-tag in the header, with an ID and a runat="server"... then in the code behind say

或者你可以把你的元标记放在标题中,带有一个 ID 和一个 runat="server"...然后在后面的代码中说

myMetaTag.Content = "noindex,follow";

or

或者

myMetaTag.Visible = false;

or whatever you'd like.

或者任何你想要的。

回答by Jonathan Williams

Many thanks to Awe for the solution! I have implemented this code in a (error404.ascx) ASP.NET User Control as follows:

非常感谢Awe 的解决方案!我已经在 (error404.ascx) ASP.NET 用户控件中实现了这段代码,如下所示:

<%@ Control Language="C#"%>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.TrySkipIisCustomErrors = true;  //Suppress IIS7 custom errors
        Response.StatusCode = 404;
        SetRobotsHeaderMetadata();
    }

    private void SetRobotsHeaderMetadata()
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Name = "robots";
        meta.Content = "noindex,follow";
        this.Page.Master.FindControl("cphPageMetaData").Controls.Add(meta);
    }
</script>

With the following masterpage:

使用以下母版页:

<%@ Master Language="C#" AutoEventWireup="true" Inherits="MyMaster" %>
<script runat="server">
    ...
</script>

<!DOCTYPE html>
<html lang="en-GB">
    <head>
        <title>Site title here</title>

        <asp:contentplaceholder runat="server" id="cphPageMetaData">
        </asp:contentplaceholder>
    </head>

    <body>
        ...
    </body>
</html>

回答by hemantrautela

The best solution for this, which I successfully checked without any error or warning:

对此的最佳解决方案,我已成功检查,没有任何错误或警告:

The JavaScript code, which contains the <% ... %>code, was removed from the headsection and placed in the bodysection.

包含<% ... %>代码的 JavaScript 代码已从该head部分中删除并放置在该body部分中。

回答by George Filippakos

You could define your meta tag as a static string like so:

您可以将元标记定义为静态字符串,如下所示:

Private Shared MetaLanguage As String =
    String.Format("<meta http-equiv=""Content-Language"" content=""{0}""/>", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)

Then place them in your head like so:

然后像这样把它们放在你的头上:

<head runat="server">
    <%=MetaLanguage%>
</head>

This allow you to use any meta tag values and is easy to read and customize. Note: The use of the Sharedkeyword (static) helps improve performance.

这允许您使用任何元标记值,并且易于阅读和自定义。注意:使用Shared关键字 (static) 有助于提高性能。

回答by Nathan

MetaDescription = "Your meta description goes here"; MetaKeywords = "Keyword1,Keyword2,Keyword3";

MetaDescription = "你的元描述在这里"; MetaKeywords = "Keyword1,Keyword2,Keyword3";

回答by SmartDev

I think this is the best approach:

我认为这是最好的方法:

this.Page.Header.Controls.Add(new LiteralControl(@"<meta ... />"));

Enjoy!

享受!

回答by user3425953

OK... I actually only use C#... Or HTML into C#. I never use codebehind, designer or webcontrols in the file aspx...So I program everythingfrom classes... And dynamically.

好吧...我实际上只使用 C#...或 HTML 转换为 C#。我从不在文件 aspx 中使用代码隐藏、设计器或 web 控件……所以我从类中编写所有内容……并且是动态的。

Result:

结果:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
`meta.Content = "Here is what you want";`
var page=HttpContext.Current.Handler as Page;
page.Header.Controls.Add(meta);