动态创建站点地图并将其路由到根级别
我正在尝试为我的网站动态创建站点地图,但我不希望它(我相信有必要拥有它)位于根级别,即。 Domain.com/sitemap.xml 到目前为止,我在主控制器中的操作结果如下:
public ActionResult SiteMap()
{
...
return this.Content(xml, "text/xml");
}
这对于创建文件来说效果很好,但它位于 Domain.com/Home/Sitemap ,我的理解是站点地图应该位于根级别。有没有办法重写 url 或在 Global.asax 文件中为其添加新路由?默认情况下,Global.asax 文件包含以下内容:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
所以看起来这可能是添加一些内容的地方,例如检查请求的路由是否为 Sitemap.xml 并将其重定向到 Home/sitemap(或者只是在那里提供站点地图) 。如果可能的话,我不知道如何做到这一点,因为我从来没有搞乱过 RegisterRoutes 部分。
可能出现的另一个问题是扩展名 .xml,我认为 IIS 会认为这是驱动器上的文件并忽略所有路由。
我是否走在正确的道路上,或者有更好的方法吗?或者这是不可能的,我必须只物理创建文件?
编辑(已解决) 我突然意识到我一直在以错误的方式思考这个问题。我所要做的就是有一种方法来创建/更新站点地图文件并将其定期或每次添加页面时保存到 /sitemap.xml 。我不敢相信这对我来说并不明显。
I am trying to dynamically create a sitemap for my site and I wan't it to be (I believe it is necessary to have it) located at the root level, ie. Domain.com/sitemap.xml
So far I have an action result in the home controller like this:
public ActionResult SiteMap()
{
...
return this.Content(xml, "text/xml");
}
This works fine for creating the file but it is located at Domain.com/Home/Sitemap and my understanding is the the sitemap is supposed to be located at the root level. Is there a way to rewrite the url or add a new route for it in the Global.asax file? By default the Global.asax file contains this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So it seems like this may be the place to add something like a check if the requested route is Sitemap.xml and just redirect it to Home/sitemap (or just serve the sitemap right there). I'm not sure how to do this, if it is possible, as I have never messed around with the RegisterRoutes section.
Another problem that may arise would be the extension .xml, I would think that IIS would think this is a file on the drive and ignore the route all together.
Am I going down the correct path, or is there a better way to do this? Or is it not possible and I have to just physically create the file?
Edit (Solved)
I just occurred to me that I have been thinking about this the wrong way. All I would have to do is have a method to create/update a sitemap file and save it to /sitemap.xml periodicity or every time a page is added. I can't believe this wasn't obvious to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加一条路由:
并确保将此路由放在默认路由之前。
You could add a route:
and make sure that you put this route before your default route.
这可能有点偏离主题,(但同时,有点接近......我认为)看看 MVCSitemapProvider 。
它非常简单,可以在项目中使用,我最近开始使用它,它的作用就像一个魅力。如果您最终想要走这条路,我可以添加一些代码示例?
This might be slightly off topic, (but in the same breath, kind of close... I think) Have a look at MVCSitemapProvider.
It's simple enough to use in a project, I started using it lately and it works like a charm. I can add some code samples if you do end up wanting to go this route?