net mvc -不同的视图在布局页面的中需要不同的元标签

时间:2021-02-09 03:27:36

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:

我想阻止我的一些页面出现在搜索结果中。我的理解是,我将以下内容添加到页面的部分:

<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.
  • 使用机器人。txt比我想要的更糟糕。
  • 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.
  • 试图将模型传递到布局中是有点过分的(需要所有的模型从一些基础继承,许多页面都是纯粹的表示,甚至没有模型),而且是糟糕的。

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.

希望我遗漏了一些东西,有一种很好的(不错的)方法可以做到这一点,或者我上面提到的其中一种方法也不是那么糟糕。

5 个解决方案

#1


64  

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

由于所有的刀片服务器都是经过处理的服务器端,所以在a中没有问题,因为一些爬行器没有实现JS和b),也没有延迟到标签/等等。另外,被标记为不需要意味着您只需更新不想被索引的页面,并且不必在应用程序的每个页面上设置一个变量。

#2


8  

You can add the following conditional with the meta-tag to the <head> element in your common layout:

您可以在公共布局中向元素添加具有元标记的条件:

<!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文件,视图文件夹中的文件。这意味着默认情况下没有页面会添加这个元标签。这将是_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[" disableindex "] = 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 answer if you prefer to use the ViewBag.

顺便说一句,您还可以使用ViewBag将数据从_ViewStart传递到布局,但是代码有点难看,因为您无法直接访问ViewStart中的ViewBag。如果您喜欢使用ViewBag,请查看这个答案。

#3


6  

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.

所以你可以在你的页面中单独定义一个特定的元标签和其他东西。

#4


0  

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"/>');

#5


0  

Old question, but if it may helps someone, in the upper Layout, I had to use :

老问题,但如果它可以帮助某人,在上面的布局中,我必须使用:

<head>
    @RenderSection("MySection", required:false)
</head>

Then, in every nested Layouts, I had to redefine my section :

然后,在每个嵌套布局中,我必须重新定义我的部分:

@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> *@
}

#1


64  

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

由于所有的刀片服务器都是经过处理的服务器端,所以在a中没有问题,因为一些爬行器没有实现JS和b),也没有延迟到标签/等等。另外,被标记为不需要意味着您只需更新不想被索引的页面,并且不必在应用程序的每个页面上设置一个变量。

#2


8  

You can add the following conditional with the meta-tag to the <head> element in your common layout:

您可以在公共布局中向元素添加具有元标记的条件:

<!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文件,视图文件夹中的文件。这意味着默认情况下没有页面会添加这个元标签。这将是_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[" disableindex "] = 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 answer if you prefer to use the ViewBag.

顺便说一句,您还可以使用ViewBag将数据从_ViewStart传递到布局,但是代码有点难看,因为您无法直接访问ViewStart中的ViewBag。如果您喜欢使用ViewBag,请查看这个答案。

#3


6  

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.

所以你可以在你的页面中单独定义一个特定的元标签和其他东西。

#4


0  

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"/>');

#5


0  

Old question, but if it may helps someone, in the upper Layout, I had to use :

老问题,但如果它可以帮助某人,在上面的布局中,我必须使用:

<head>
    @RenderSection("MySection", required:false)
</head>

Then, in every nested Layouts, I had to redefine my section :

然后,在每个嵌套布局中,我必须重新定义我的部分:

@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> *@
}