ASP.NET MVC 3 中的元标记

发布于 2024-12-06 17:53:28 字数 58 浏览 1 评论 0原文

如何让元标记仅适用于一页。如果我想把它放在.aspx文件中,哪里是正确的位置。

谢谢。

How can I put meta tag to work only for one page. If I want to put it .aspx file, where is right place.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-12-13 17:53:28

由于您还没有说过,我假设您正在使用 Razor 引擎(新 MVC3 项目的“默认”引擎)。在这种情况下,您只需在布局视图中插入一个新部分,并且仅在需要插入元标记时才渲染该部分。

例如,使用现有的 New ASP.NET MVC 3 项目模板,您可以编辑 Views\Shared\_Layout.cshtml 文件,并在结束 之前编辑该文件。 code> 标记,执行如下操作:

    @this.RenderSection("MetaContent", false)
</head>

然后,在您需要的任何视图中添加以下内容:

@section MetaContent
{
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
}

如果由于某种原因您仍在使用 ASPX 布局引擎,则可以使用母版页中的 标记和视图中的 标记。

编辑:

由于您仍在使用 ASP.NET Forms 布局引擎,因此在 aspx 语法中,这里的基本思想与上面相同:

在母版页中,添加标签:

    <asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</head>

在 .aspx 视图中,添加新的内容部分(您应该至少有两个——标题和正文):

<asp:Content ID="Meta" ContentPlaceHolderID="MetaContent" runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
</asp:Content>

Since you haven't said yet, I'm assuming you're using the Razor engine (the "default" for new MVC3 projects). In that case, you just need to insert a new section into your layout view, and only render that section if you need to insert a meta tag.

For example, working from the stock New ASP.NET MVC 3 Project template, you would edit your Views\Shared\_Layout.cshtml file, and before the closing </head> tag, do something like this:

    @this.RenderSection("MetaContent", false)
</head>

Then, in any of your views that you needed to, add this:

@section MetaContent
{
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
}

If you're still using the ASPX layout engine for some reason, you can accomplish the same thing using the <asp:ContentPlaceHolder> tags in your master page and <asp:Content> tags in your views.

EDIT:

Since you're using the ASP.NET Forms layout engine still, here's the same basic idea as above in aspx syntax:

In your master page, you add the tag:

    <asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</head>

And in your .aspx views, you add a new content section (you should already have at least two -- a title and a body):

<asp:Content ID="Meta" ContentPlaceHolderID="MetaContent" runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
</asp:Content>
挽清梦 2024-12-13 17:53:28

我打算准确地建议迈克尔所说的(+1)。另一种选择是在 ViewBag 中放置一个布尔值,例如:

ViewBag.ForceIE8Mode = true;

对于您想要强制进入 IE8 模式的页面。

然后在您看来,将元标记包装在条件中。要么

@if(ViewBag.ForceIE8Mode == true) {
    <meta... />
}

或者

<% if(ViewBag.ForceIE8Mode == true) { %>
    <meta... />
<% } %>

I was going to suggest exactly what Michael said (+1). Another option would be to put a boolean in the ViewBag, something like:

ViewBag.ForceIE8Mode = true;

For pages that you want to force into IE8 Mode.

and then in your view, wrap the meta tag in a conditional. either

@if(ViewBag.ForceIE8Mode == true) {
    <meta... />
}

or

<% if(ViewBag.ForceIE8Mode == true) { %>
    <meta... />
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文