---
title: "Sitemap in Asp.Net MVC"  
description: "Sitemap in Asp.Net MVC"  
author: "Anonymous User"  
published: 2014-10-10  
updated: 2014-10-10  
canonical: https://www.mindstick.com/forum/2360/sitemap-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc"]  
reading_time: 2 minutes  

---

# Sitemap in Asp.Net MVC

'm trying to create an [automatic](https://www.mindstick.com/forum/145518/what-automatic-repeat-request-arq) sitemap [ActionResult](https://www.mindstick.com/forum/33961/what-is-the-difference-between-actionresult-and-viewresult) that outputs a valid sitemap.[xml file](https://www.mindstick.com/forum/360/how-to-read-xml-file-in-php). The actual [generation](https://www.mindstick.com/blog/300788/why-is-heart-attack-increasing-in-the-younger-generation) of the file is not a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic), but I can't seem to figure out how to [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) the list of URL's in the system. Here is the code I have so far:

```
public ContentResult Sitemap()        {            XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";            XElement root = new XElement(xmlns + "urlset");             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);            }        }
```

For [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance), suppose I have two [controllers](https://www.mindstick.com/forum/155773/how-asynchronous-controllers-work-in-asp-dot-net-mvc), and each [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) has two actions associated with them:

**HelpController**

· Edit

· Create

**AboutController**

· Company

· [Management](https://www.mindstick.com/articles/23490/tips-for-better-cash-flow-management)

## Replies

### Reply by Anonymous User

I took a look at Maarten Balliauw's approach per likwid's comment, but it seems to be overkill for what I'm trying to do.

I've hacked together a temporary solution. I'm simply passing the controller and action names to generate the URL's. In order to generate the URL's, I'm using the following code:

```
    List<string> urlList = new List<string>();    urlList.Add(GetUrl(new { controller = "Help", action = "Edit" }));    urlList.Add(GetUrl(new { controller = "Help", action = "Create" }));    urlList.Add(GetUrl(new { controller = "About", action = "Company" }));    urlList.Add(GetUrl(new { controller = "About", action = "Management" }));
```

where GetUrl is as below:\

```
protected string GetUrl(object routeValues)        {            RouteValueDictionary values = new RouteValueDictionary(routeValues);            RequestContext context = new RequestContext(HttpContext, RouteData);             string url = RouteTable.Routes.GetVirtualPath(context, values).VirtualPath;             return new Uri(Request.Url, url).AbsoluteUri;        }
```

This seems to do the trick for now, though I do like the idea of having actionfilter's applied to certain actions that get pulled together automatically.\


---

Original Source: https://www.mindstick.com/forum/2360/sitemap-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
