title: Dynamic sitemap in ASP.NET MVC
body: <p>I'm trying to create an automatic sitemap ActionResult that outputs a valid sitemap.xml file.  The actual generation of the file is not a problem, but I can't seem to figure out how to populate the list of URL's in the system.  Here is the code I have so far: </p>
  
  <pre><code>    public ContentResult Sitemap()
      {
          XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
          XElement root = new XElement(xmlns + "urlset");
  
          //some kind of foreach here to get the loc variable for all URLs in the site
          //for each URL in the collection, add it to the root element as here
  
          //root.Add(
          //    new XElement("url", 
          //        new XElement("loc", "http://google.com"), 
          //        new XElement("changefreq", "daily")));
  
          using (MemoryStream ms = new MemoryStream())
          {
              using (StreamWriter writer = new StreamWriter(ms, Encoding.UTF8))
              {
                  root.Save(writer);
              }
  
              return Content(Encoding.UTF8.GetString(ms.ToArray()), "text/xml", Encoding.UTF8);
          }
      }
  </code></pre>
  
  <p>For instance, suppose I have two controllers, and each controller has two actions associated with them: </p>
  
  <p><strong>HelpController</strong></p>
  
  <ul>
  <li>Edit</li>
  <li>Create</li>
  </ul>
  
  <p><strong>AboutController</strong></p>
  
  <ul>
  <li>Company</li>
  <li>Management</li>
  </ul>
  
  <p>I can't seem to figure out how to get a list of URL's like:</p>
  
  <ul>
  <li><a href="http://localhost/help/edit" rel="noreferrer">http://localhost/help/edit</a></li>
  <li><a href="http://localhost/help/create" rel="noreferrer">http://localhost/help/create</a></li>
  <li><a href="http://localhost/about/company" rel="noreferrer">http://localhost/about/company</a></li>
  <li><a href="http://localhost/about/management" rel="noreferrer">http://localhost/about/management</a></li>
  </ul>