Html asp.net mvc - 不同的视图在布局页面内的 <head> 中需要不同的元标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20993317/
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
asp.net mvc - different views need different meta tag in <head> inside layout page
提问by acarlon
I would like to stop a few of my pages from showing up in search results. My understanding is that I add the following to the <head>
section of the page:
我想阻止我的一些页面出现在搜索结果中。我的理解是我将以下内容添加到<head>
页面的部分:
<meta name="robots" content="noindex,nofollow"/>
The problem is that my pages use a common Layout page. Something like:
问题是我的页面使用了一个通用的布局页面。就像是:
@{
Layout = "~/Views/Shared/_VanillaLayout.cshtml";
}
Inside the layout page is the head section with a whole lot of links, scripts and meta tags. I don't want to duplicate this for indexable and non-indexable pages.
布局页面内部是带有大量链接、脚本和元标记的头部部分。我不想为可索引和不可索引的页面复制这个。
From my research I have found that: -
从我的研究中我发现:-
- Having multiple
<head>
sections is bad. - Having the robot meta tag outside of head is bad.
- Using robots.txt is more than I want and is bad.
- Trying to pass a model into the layout is a bit of an overkill (need all models to inherit from some base and many pages are purely presentation and don't even have a model) and is bad.
- 有多个
<head>
部分是不好的。 - 将机器人元标记放在头部之外是不好的。
- 使用 robots.txt 比我想要的多而且不好。
- 尝试将模型传递到布局中有点矫枉过正(需要所有模型从某些基础继承,许多页面纯粹是演示文稿,甚至没有模型)并且很糟糕。
Hopefully, I am missing something and there is a good (non-bad) way to do this or one of the approaches I have mentioned above is not so bad after all.
希望我遗漏了一些东西,并且有一种好的(非坏的)方法可以做到这一点,或者我上面提到的其中一种方法毕竟还不错。
回答by Tommy
It seems to me the easiest way would be to define a section in the <head>
tag of your layout file that you can choose to populate with data in your views
在我看来,最简单的方法是在<head>
布局文件的标签中定义一个部分,您可以选择在视图中填充数据
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<!-- Adding a RenderSection here, mark it as not required-->
@RenderSection("AdditionalMeta", false)
@Styles.Render("~/Content/css")
</head>
Now, in any view in which you need to add additional meta data, simply add the following code at the end/beginning (after model declarations) of your view file
现在,在任何需要添加额外元数据的视图中,只需在视图文件的末尾/开头(在模型声明之后)添加以下代码
@section AdditionalMeta
{
<meta name="robots" content="noindex,nofollow"/>
}
Since all of the Razor stuff is processed server side, there would be no issues in a) having JS append items given that some crawlers do not implement JS and b)no late appending to <head>
tag/etc. Also, being marked as not required means that you only have to update the pages that you want to not be indexed and not have to set a variable on every single page in your application.
由于所有 Razor 内容都是在服务器端处理的,因此在 a) 有 JS 附加项目时不会有问题,因为某些爬虫没有实现 JS,并且 b) 没有延迟附加到<head>
标签/等。此外,被标记为不需要意味着您只需要更新不想被索引的页面,而不必在应用程序的每个页面上设置变量。
回答by Daniel J.G.
You can add the following conditional with the meta-tag to the <head>
element in your common layout:
您可以将以下带有元标记的条件添加到<head>
公共布局中的元素:
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@if (PageData["DisableIndexing"])
{
<meta name="robots" content="noindex,nofollow"/>
}
...
</head>
<body>
...
</body>
That flag will be set as disabled by default in your main _ViewStart.cshtml
file, the one in the Views folder. That would mean by default no page will add that meta tag. This will be the _ViewStart file:
默认情况下,该标志将在您的主_ViewStart.cshtml
文件(即 Views 文件夹中的文件)中设置为禁用。这意味着默认情况下没有页面会添加该元标记。这将是 _ViewStart 文件:
@{
Layout = "~/Views/Shared/_VanillaLayout.cshtml";
PageData["DisableIndexing"] = false;
}
Finally, on pages where you want to disable indexing you just need to override that flag. For example if the Foo view should not allow indexing, you would do:
最后,在您想要禁用索引的页面上,您只需要覆盖该标志。例如,如果 Foo 视图不允许索引,你可以这样做:
@model MyNamespace.MyFooModel
@{
ViewBag.Title = "Foo";
PageData["DisableIndexing"] = true;
}
...
If all the views within a certain folder should disable indexing, you could even add another _ViewStart.cshtml file to that folder where you would just set PageData["DisableIndexing"] = true;
如果某个文件夹中的所有视图都应该禁用索引,您甚至可以将另一个 _ViewStart.cshtml 文件添加到您刚刚设置的文件夹中 PageData["DisableIndexing"] = true;
As a side note, you could also use the ViewBag to pass data from the _ViewStart to the layout, but code is a bit ugly as you don't have direct access to the ViewBag in the ViewStart. See this answerif you prefer to use the ViewBag.
作为旁注,您还可以使用 ViewBag 将数据从 _ViewStart 传递到布局,但代码有点难看,因为您无法直接访问 ViewStart 中的 ViewBag。如果您更喜欢使用 ViewBag,请参阅此答案。
回答by Dilip0165
If you are not defining any meta tag in Layout page and you simply want to add from your Page then you can do as following.
如果您没有在布局页面中定义任何元标记,而只想从您的页面添加,那么您可以执行以下操作。
in your layout page _VanillaLayout.cshtml under head section use @RenderSection as following
在您的布局页面 _VanillaLayout.cshtml 下 head 部分使用 @RenderSection 如下
<head>
<meta charset="utf-8">
@RenderSection("SeoRender", false)
</head>
Now in your view page do following way
现在在您的视图页面中执行以下操作
@{
Layout = "~/Views/Shared/_VanillaLayout.cshtml";
}
@section SeoRender{
@{
<title>testTitle</title>
<meta name="keyword" content="testkeyword">
<meta name="description" content="testdescription">
<meta name="author" content="testauthor">
}
So this you can define specifican meta tag and other thing individually in your page.
因此,您可以在页面中单独定义特定的元标记和其他内容。
回答by AlexB
Old question, but if it may helps someone, in the upper Layout, I had to use :
老问题,但如果它可以帮助某人,在上面的 Layout 中,我不得不使用:
<head>
@RenderSection("MySection", required:false)
</head>
Then, in everynested Layouts, I had to redefine my section :
然后,在每个嵌套的 Layouts 中,我不得不重新定义我的部分:
@section MySection {
@RenderSection("MySection", false)
}
Finally, I defined my section in my .cshtml
view :
最后,我在我的.cshtml
观点中定义了我的部分:
@section MySection{
<meta name="robots" content="@Model.MetaContent"/>
@* or any other tag going to <head> *@
}
回答by theLaw
Try with Jquery, in the page that you don't want to be indexed, add
用jquery试试,在不想被索引的页面,添加
$('head').append('<meta name="robots" content="noindex,nofollow"/>');
Edit:
编辑:
another try could be (according to this Will Googlebot crawl changes to the DOM made with JavaScript?) to try with simple javascript instead of jquery library
另一个尝试可能是(根据此Googlebot 是否会抓取对使用 JavaScript 所做的 DOM 的更改?)尝试使用简单的 javascript 而不是 jquery 库
document.getElementsByTagName('head')[0].appendChild('<meta name="robots" content="noindex,nofollow"/>');