MvcSiteMapProvider MVC5 CanonicalUrl

百般思念 提交于 2020-01-05 09:12:42

问题


I can't figure out what is wrong with MVC.SiteMap, it does not display CanonicalUrl when I check the page source. In _layout file I have the following:

@Html.MvcSiteMap().CanonicalTag()
@Html.MvcSiteMap().MetaRobotsTag()

Here is MVC.Sitemap:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

  <mvcSiteMapNode title="Home" controller="Home" action="Index"  metaRobotsValues="noindex" CanonicalUrl="/Home/Index">
    <mvcSiteMapNode title="Healthcare Services" controller="Home" action="Healthcare" metaRobotsValues="noindex follow"  CanonicalUrl="/Home/Healthcare"/>
    <mvcSiteMapNode title="About" controller="Home" action="About"  metaRobotsValues="noindex" CanonicalUrl="/Home/About"/>
    <mvcSiteMapNode title="Training" controller="Home" action="Training" metaRobotsValues="noindex follow"  canonicalUrl="/Home/Training"/>
    <mvcSiteMapNode title="Environmental Services" controller="Home" action="Environment" metaRobotsValues="follow" canonicalUrl="/Home/Environment"/>
    <mvcSiteMapNode title="IT Services" controller="Home" action="Internet" metaRobotsValues="noindex"  CanonicalUrl="/Home/Internet"/>
    <mvcSiteMapNode title="Our Clients" controller="Home" action="Clients" metaRobotsValues="noindex noarchive"  canonicalUrl="/Home/Clients"/>
    <mvcSiteMapNode title="Privacy" controller="Home" action="Privacy" metaRobotsValues="noindex noarchive" canonicalUrl="/Home/Privacy"/>
  </mvcSiteMapNode>
</mvcSiteMap>

Would appreciate your suggestions.


回答1:


The canonical tag is used to indicate that 2 URLs point to the same resource (usually another page with the same or very similar content).

In your configuration, you are configuring each node with its own controller and action, with no indication that there is another node with identical content. Since each node is its own canonical URL the tag will not be shown. It is only displayed on alternate pages with a matching canonicalUrl or canonicalKey. For example, if your /Home/Training and /Home/Environment actions both serve the same content and you want to specify that /Home/Training is the original and /Home/Environment is the copy, you configure the nodes like this:

<mvcSiteMapNode title="Training" controller="Home" action="Training" metaRobotsValues="noindex follow"/>
<mvcSiteMapNode title="Environmental Services" controller="Home" action="Environment" metaRobotsValues="follow" canonicalUrl="/Home/Training"/>

You will then see the canonical tag generated only for the /Home/Environment page with an absolute URL of the /Home/Training page.

However, a more maintainable option is to use canonicalKey instead of canonicalUrl. That way, if the URL changes on /Home/Training you don't have to update it for every node that specifies it as the canonical node.

<mvcSiteMapNode title="Training" controller="Home" action="Training" metaRobotsValues="noindex follow" key="Home_Training"/>
<mvcSiteMapNode title="Environmental Services" controller="Home" action="Environment" metaRobotsValues="follow" canonicalKey="Home_Training"/>

CanonicalUrl is there in case you are specifying canonical pages that are not hosted by MVC or are external to the website.



来源:https://stackoverflow.com/questions/25895766/mvcsitemapprovider-mvc5-canonicalurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!