为我的网站自动生成 sitemap.xml
有人可以指导我如何使用 ashx 处理程序为我的网站自动生成 sitemap.xml 文件吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有人可以指导我如何使用 ashx 处理程序为我的网站自动生成 sitemap.xml 文件吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
假设您网站上的每个唯一的
aspx
文件都对应一个页面,您将需要使用 System.IO 的*.aspx
命名空间过滤。如果您提供*.html
或*.pdf
等,您可能希望包含其他可能的文件。创建一个递归函数,迭代目录中的所有文件,然后调用本身位于目录中的所有子目录上。
当您浏览目录树时,只需在 sitemap.xml 中为每个项目生成一个节点。使用 String.Builder 执行此操作。
您的 sitemap.xml 结构 是;
将 URL 附加到单独的
元素中。Assuming that each unique
aspx
file on your site corresponds to a single page you will want to traverse the files and the folders within your site using theDirectory
objects in System.IO namespace filtering for*.aspx
. You may want to include other possible files if you serve*.html
or*.pdf
etc.Create a recursive function that iterates through all the files in a directory and then calls itself on all the subdirectories in the directory.
As you walk the directory tree simply generate a node in sitemap.xml for each item. Do this with String.Builder.
Your sitemap.xml structure is;
Append the URLs in separate
<loc/>
elements.