How to add blog posts to sitemap.xml?

3 years ago
I'm trying to add blogPosts to the sitemap by adding following code in SitemapGenerator.cs

protected virtual IEnumerable<SitemapUrl> GetBlogUrls(UrlHelper urlHelp)
        {
            return _blogService.GetAllBlogPosts(storeId: 0, languageId: 0, dateFrom: null, dateTo: null, pageIndex: 0, pageSize: int.MaxValue, showHidden: false).Select (blog =>
            {
                var url = urlHelp.RouteUrl("Blog", new { blogPostId = blog.Id, SeName = blog.GetSeName() }, GetHttpProtocol());
                return new SitemapUrl(url, UpdateFrequency.Weekly, blog.CreatedOnUtc);
            });
        }

But the return result is
<url>
<loc>http://localhost/blog?blogPostId=3</loc>
<changefreq>weekly</changefreq>
<lastmod>2017-05-03</lastmod>
</url>
<url>
<loc>http://localhost/blog?blogPostId=1</loc>
<changefreq>weekly</changefreq>
<lastmod>2017-04-20</lastmod>
</url>

The return number is write, but not turn to the correct URLs.
Anyone help?

Thanks
3 years ago
zhugefs wrote:
I'm trying to add blogPosts to the sitemap by adding following code in SitemapGenerator.cs

protected virtual IEnumerable<SitemapUrl> GetBlogUrls(UrlHelper urlHelp)
        {
            return _blogService.GetAllBlogPosts(storeId: 0, languageId: 0, dateFrom: null, dateTo: null, pageIndex: 0, pageSize: int.MaxValue, showHidden: false).Select (blog =>
            {
                var url = urlHelp.RouteUrl("Blog", new { blogPostId = blog.Id, SeName = blog.GetSeName() }, GetHttpProtocol());
                return new SitemapUrl(url, UpdateFrequency.Weekly, blog.CreatedOnUtc);
            });
        }

But the return result is
<url>
<loc>http://localhost/blog?blogPostId=3</loc>
<changefreq>weekly</changefreq>
<lastmod>2017-05-03</lastmod>
</url>
<url>
<loc>http://localhost/blog?blogPostId=1</loc>
<changefreq>weekly</changefreq>
<lastmod>2017-04-20</lastmod>
</url>

The return number is write, but not turn to the correct URLs.
Anyone help?

Thanks


Try===>

protected virtual IEnumerable<SitemapUrl> GetBlogUrls(UrlHelper urlHelp)
        {
            return _blogService.GetAllBlogPosts(storeId: 0, languageId: 0, dateFrom: null, dateTo: null, pageIndex: 0, pageSize: int.MaxValue, showHidden: false).Select (blog =>
            {
                var url = urlHelp.RouteUrl("BlogPost", new { SeName= blog.GetSeName(blog.LanguageId, ensureTwoPublishedLanguages: false) }, GetHttpProtocol());
                return new SitemapUrl(url, UpdateFrequency.Weekly, blog.CreatedOnUtc);
            });
        }



Note: v3.9, others will similar too, may have some change on blog.GetSeName(blog.LanguageId, ensureTwoPublishedLanguages: false)
3 years ago
Sohel, you are great, it works, thank you very much.

Z.