Reputation: 4458
I am trying to dynamically create a sitemap for my site and I wan't it to be (I believe it is necessary to have it) located at the root level, ie. Domain.com/sitemap.xml So far I have an action result in the home controller like this:
public ActionResult SiteMap()
{
...
return this.Content(xml, "text/xml");
}
This works fine for creating the file but it is located at Domain.com/Home/Sitemap and my understanding is the the sitemap is supposed to be located at the root level. Is there a way to rewrite the url or add a new route for it in the Global.asax file? By default the Global.asax file contains this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So it seems like this may be the place to add something like a check if the requested route is Sitemap.xml and just redirect it to Home/sitemap (or just serve the sitemap right there). I'm not sure how to do this, if it is possible, as I have never messed around with the RegisterRoutes section.
Another problem that may arise would be the extension .xml, I would think that IIS would think this is a file on the drive and ignore the route all together.
Am I going down the correct path, or is there a better way to do this? Or is it not possible and I have to just physically create the file?
Edit (Solved) I just occurred to me that I have been thinking about this the wrong way. All I would have to do is have a method to create/update a sitemap file and save it to /sitemap.xml periodicity or every time a page is added. I can't believe this wasn't obvious to me.
Upvotes: 1
Views: 3017
Reputation: 5403
This might be slightly off topic, (but in the same breath, kind of close... I think) Have a look at MVCSitemapProvider.
It's simple enough to use in a project, I started using it lately and it works like a charm. I can add some code samples if you do end up wanting to go this route?
Upvotes: 1
Reputation: 1039438
You could add a route:
routes.MapRoute(
"sitemap",
"sitemap.xml",
new { controller = "Home", action = "SiteMap" }
);
and make sure that you put this route before your default route.
Upvotes: 2