sitemaps.xml in asp.net mvc

2019-04-15 05:22发布

问题:

My website has dynamic content, new links are being created.

my db has a table which pretty much contains all the added urls.

So my question is how importnt is it for me to have sitemaps.xml, and also is there a simple way to build it so that when new links are generated i can tag it to the end of a sitemap.xml file?

回答1:

You can use LINQ to XML to create an XML sitemap from your database, then return the sitemap from an action by returning Content(document.ToString(), "text/xml").



回答2:

It is important for crawlers to be able to crawl your site quicker and with more accurate.

You can create a controller, say siteMapController and in Index add the following

 public ActionResult Index() {
        var xmlString =
            "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";

        xmlString +=
                "<url>" +
               "<loc>Your site</loc>" +
               "<changefreq>weekly</changefreq>" +
               "<priority>0.6</priority>" +
             "</url>" +
           "<url>" +
                    "<loc>Static Link of you site</loc>" + //you can add as many you want
                       "<changefreq>weekly</changefreq>" +
                       "<priority>0.6</priority>" +
                    "</url>" +
                    "<url>";


           //Dynamic links
            xmlString += "<url>" +
                         "<loc>Link of new item"</loc>" +
                         "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" +
                         "<changefreq>daily</changefreq>" +
                         "<priority>0.8</priority>" +
                         "</url>";
        }
        xmlString += "</urlset>";
        ViewData["siteMap"] = xmlString;
        return View();
    }

save the xml on your server and post the sitemap via this link

https://www.google.com/webmasters/tools/home?hl=en

hope that helps