Skip to content
PureKrome edited this page Dec 3, 2014 · 1 revision

Finally, the search engine requires a link to the real, actual resource it is suppose to index.

This is where the <urlset/> nodes come into play. So, given a small page of data, lets display the links to the actual resource to index:

Setup

    public ContentResult GetProductsSitemap(int page)
    {
        // Check if page is <= 0, etc..

        const int pageSize = 25000;

        // 1. Grab our *paged* data.
        //    We have 1 million products - so don't return EVERYTHING,
        //    just the 'x' number of rows, for the page.
        //    Eg. page 2 - return records 25,001 -> 50,000.
        var products = GetProductsPaged(page, pageSize);

        // 2. Convert it to a urlset collection.
        // NOTE: This creates a url/route based upon the ASP.NET Routes.
         var sitemapNodes = (from p in products
                            select new SitemapNode(
                                new Uri(Url.AbsoluteRouteUrl(
                                    "GET Products - Index", 
                                    new {p.ProductId})))
                            {
                                Frequency = SitemapFrequency.Hourly,
                                LastModified = p.LastModified,
                                Priority = 1.0
                            }).ToList();

        // 3. Create the sitemap service.
        var sitemapService = new SitemapService();

        // 4. Get the sitemap answer! BOOM!
        var xml = sitemapService.ConvertToXmlUrlset(sitemapNodes);

        // 5. Return the result as xml.
        return Content(xml, "application/xml");
    }

Result

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
            <loc>http://www.myWebSite.com/products/666</loc>
            <lastmod>2014-11-21T06:31+00:00</lastmod>
            <changefreq>hourly</changefreq>
            <priority>1</priority>
        </url>
        <url>
            <loc>http://www.myWebSite.com/products/224325</loc>
            <lastmod>2014-11-21T06:28+00:00</lastmod>
            <changefreq>hourly</changefreq>
            <priority>1</priority>
        </url>
        ....
    </urlset>