Create a site map on the fly

I am trying to create sitemap.xml on the fly for a particular asp.net website.

I found a couple of solutions:

Chinookwebs works fine, but now it seems a little inactive, and it is not possible to personalize the "priority" and "changefreq" tags of each page, they all inherit the same value from the configuration file.

What solutions do you use?

+4
source share
3 answers

Usually an HTTP handler is used for this. Given the request ...

http://www.yoursite.com/sitemap.axd

... your handler will respond with a formatted XML card. Regardless of whether this site map is created on the fly, from a database or some other method, it matches the implementation of the HTTP handler.

Here's roughly what it would look like:

void IHttpHandler.ProcessRequest(HttpContext context) { // // Important to return qualified XML (text/xml) for sitemaps // context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.ContentType = "text/xml"; // // Create an XML writer // XmlTextWriter writer = new XmlTextWriter(context.Response.Output); writer.WriteStartDocument(); writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9"); // // Now add entries for individual pages.. // writer.WriteStartElement("url"); writer.WriteElementString("loc", "http://www.codingthewheel.com"); // use W3 date format.. writer.WriteElementString("lastmod", postDate.ToString("yyyy-MM-dd")); writer.WriteElementString("changefreq", "daily"); writer.WriteElementString("priority", "1.0"); writer.WriteEndElement(); // // Close everything out and go home. // result.WriteEndElement(); result.WriteEndDocument(); writer.Flush(); } 

This code can be improved, but the main idea.

+7
source

Custom handler for creating a sitemap.

0
source

Using ASP.NET MVC simply cracked the fast part of the code using the .NET XML generation library, and then simply passed it to the view page that contained the XML control. In encoding, I associated the control with ViewData. This seemed to override the default behavior of view pages to represent a different title.

0
source

Source: https://habr.com/ru/post/1276323/


All Articles