MvcSiteMapProvider - 使用自定义 URL 访问站点地图
MvcSiteMapProvider - Using a custom URL to access the sitemap
首先我要说我是 .NET 和 C# 的新手,所以这可能是一个相对简单的问题,但到目前为止我无法自己解决它。我在自己的时间学习 .NET MVC,但由于各种情况,我不得不在工作中学习它,因此我目前对它的知识和理解是零散的,尽管到目前为止我只是通过看东西来管理向上。
我也不能给出具体的名称或 URLs,因为它与签订了保密协议的客户有关,所以我必须保持通用。
情况:
我有一个内部构建并用于客户项目的应用程序(如果您愿意,可以使用自定义 CMS)。此应用程序利用 MvcSiteMapProvider 在必要时生成面包屑,并提供 XML 站点地图供机器人抓取。
其中一个实例还通过 Akamai 进行路由,以使子域看起来像一个子文件夹,但由于各种重定向和反向代理规则,XML 站点地图在默认情况下不再可访问 URL .我已经被交给这个问题来解决,并且一直在努力寻找解决方案。
我想做的是更改站点地图的 URL,以便机器人看起来与应用程序的其余部分位于同一虚拟子目录中。
例子:
www.application.com/sitemap.xml 会变成 www.application.com/blog/sitemap.xml
我尝试过的:
1 - 我尝试专门为站点地图定义一个自定义路由,但由于我自己缺乏知识或其他因素,这种尝试也被证明是行不通的。以下代码是我为此尝试的代码:
RouteConfig.cs
routes.MapRoute(
"CustomSitemap",
"blog/sitemap.xml",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Mvc.Sitemap
<mvcSiteMapNode title="Home" controller="Home" action="Index" route="CustomSitemap">
<!-- Providers go here ->
</mvcSiteMapNode>
我所期望的是站点地图刚刚在我在 RouteConfig.cs 中定义的 URL 可用,但这并没有发生。如果它是相关的,则没有名为 HomeController.cs 的文件,但无论如何引用默认路由似乎都可以正常工作。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
2 - 我还尝试将 Mvc.Sitemap 文件移动到另一个目录以查看是否有效,但 URL 也没有改变。
3 - 我查看了 MvcSiteMapProvider 文档,但没有任何内容可以直接回答我的问题。虽然可能有一些我由于缺乏理解而忽略的东西。
感谢您查看,对于问题的模糊性质,我深表歉意。
首先,您应该禁用 MvcSiteMapProvider
添加的路由。
内部DI
<configuration>
<appSettings>
<add key="MvcSiteMapProvider_EnableSitemapsXml" value="false"/>
</appSettings>
</configuration>
外部DI
删除这一行:
// Register the Sitemaps routes for search engines
//XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
自定义XML站点地图路由
那么只需为 XmlSiteMapController
注册路由即可。您需要有 2 个才能考虑分页(当您达到 35,000 页时,它会自动使用 sitemap index 和多个站点地图页面)。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "XmlSitemap",
url: "blog/sitemap.xml",
defaults: new { controller = "XmlSiteMap", action = "Index", page = 0 }
);
routes.MapRoute(
name: "XmlSitemap-Paged",
url: "blog/sitemap-{page}.xml",
defaults: new { controller = "XmlSiteMap", action = "Index", page = 0 }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
NOTE: This example shows you how to add a subdirectory to the route. But do note that the specification is clear that you cannot use a sitemap file to reference pages above its current directory, so you should be aware of the limitations of the protocol if you decide to implement this.
The location of a Sitemap file determines the set of URLs that can be included in that Sitemap. A Sitemap file located at http://example.com/catalog/sitemap.xml can include any URLs starting with http://example.com/catalog/ but can not include URLs starting with http://example.com/images/.
首先我要说我是 .NET 和 C# 的新手,所以这可能是一个相对简单的问题,但到目前为止我无法自己解决它。我在自己的时间学习 .NET MVC,但由于各种情况,我不得不在工作中学习它,因此我目前对它的知识和理解是零散的,尽管到目前为止我只是通过看东西来管理向上。
我也不能给出具体的名称或 URLs,因为它与签订了保密协议的客户有关,所以我必须保持通用。
情况: 我有一个内部构建并用于客户项目的应用程序(如果您愿意,可以使用自定义 CMS)。此应用程序利用 MvcSiteMapProvider 在必要时生成面包屑,并提供 XML 站点地图供机器人抓取。 其中一个实例还通过 Akamai 进行路由,以使子域看起来像一个子文件夹,但由于各种重定向和反向代理规则,XML 站点地图在默认情况下不再可访问 URL .我已经被交给这个问题来解决,并且一直在努力寻找解决方案。
我想做的是更改站点地图的 URL,以便机器人看起来与应用程序的其余部分位于同一虚拟子目录中。 例子: www.application.com/sitemap.xml 会变成 www.application.com/blog/sitemap.xml
我尝试过的: 1 - 我尝试专门为站点地图定义一个自定义路由,但由于我自己缺乏知识或其他因素,这种尝试也被证明是行不通的。以下代码是我为此尝试的代码:
RouteConfig.cs
routes.MapRoute(
"CustomSitemap",
"blog/sitemap.xml",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Mvc.Sitemap
<mvcSiteMapNode title="Home" controller="Home" action="Index" route="CustomSitemap">
<!-- Providers go here ->
</mvcSiteMapNode>
我所期望的是站点地图刚刚在我在 RouteConfig.cs 中定义的 URL 可用,但这并没有发生。如果它是相关的,则没有名为 HomeController.cs 的文件,但无论如何引用默认路由似乎都可以正常工作。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
2 - 我还尝试将 Mvc.Sitemap 文件移动到另一个目录以查看是否有效,但 URL 也没有改变。
3 - 我查看了 MvcSiteMapProvider 文档,但没有任何内容可以直接回答我的问题。虽然可能有一些我由于缺乏理解而忽略的东西。
感谢您查看,对于问题的模糊性质,我深表歉意。
首先,您应该禁用 MvcSiteMapProvider
添加的路由。
内部DI
<configuration>
<appSettings>
<add key="MvcSiteMapProvider_EnableSitemapsXml" value="false"/>
</appSettings>
</configuration>
外部DI
删除这一行:
// Register the Sitemaps routes for search engines
//XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
自定义XML站点地图路由
那么只需为 XmlSiteMapController
注册路由即可。您需要有 2 个才能考虑分页(当您达到 35,000 页时,它会自动使用 sitemap index 和多个站点地图页面)。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "XmlSitemap",
url: "blog/sitemap.xml",
defaults: new { controller = "XmlSiteMap", action = "Index", page = 0 }
);
routes.MapRoute(
name: "XmlSitemap-Paged",
url: "blog/sitemap-{page}.xml",
defaults: new { controller = "XmlSiteMap", action = "Index", page = 0 }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
NOTE: This example shows you how to add a subdirectory to the route. But do note that the specification is clear that you cannot use a sitemap file to reference pages above its current directory, so you should be aware of the limitations of the protocol if you decide to implement this.
The location of a Sitemap file determines the set of URLs that can be included in that Sitemap. A Sitemap file located at http://example.com/catalog/sitemap.xml can include any URLs starting with http://example.com/catalog/ but can not include URLs starting with http://example.com/images/.