0

I've been struggling with this for a couple of days now. Can't find any good example, or an example that I understand.

Background: I own a small blog platform for user to blog. Each user gets their own subdomain and for now there is no sitemap available. Not good. I want to create some kind of dynamic sitemap, where all sitemapnodes is retreived from the database. The sitemap will be used only for the search engine spiders. System: ASP.NET, mySQL.

The sitemap is pure XML. So I need in some way to create an ASPX file that return xml-data instead of html. And I need to somehow redirect the web.sitemap to that dynamic file.

I have never worked with XML, and I dont know how to create a file that creates XML data. So i dont even know what to search for. I don't want any static sitemap file to be stored on the server. Everything should be created on the fly.

So. Please. If you can give me some advise about XML, any example on the internet, or just what to search for.

My main questions:

1. How to create XML output from aspx file?

2. How do I "inform" the system, and search engine crawlers that the file to crawl is "/sitemap.aspx"

ThankS!

Easyrider
  • 3,199
  • 5
  • 22
  • 32
  • 1
    I believe following article would help you [http://www.codeproject.com/Articles/16860/Building-a-dynamic-SiteMap-in-ASP-NET-2-0-for-a-la](http://www.codeproject.com/Articles/16860/Building-a-dynamic-SiteMap-in-ASP-NET-2-0-for-a-la) – mqpasta Dec 27 '12 at 20:35
  • mqpasta>>> If you post that link as an answer I will flag it as answer to my question. If you want to :D – Easyrider Jan 01 '13 at 18:34

1 Answers1

0

I looked into MvcSiteMapProvider.MVC5 and I could not get it to work. First of all it modified my Web.config to the point that my css and js files were getting a 404 not found when running my web app.

With the time I spent getting MvcSiteMapProvider to work I could have just wrote my own.

So... here is my own dumbed down version of generating a sitemap xml. The only thing is you have to specify your routes manually. I haven't added reflection yet to go through each controller and pull out each action.

The data-driven piece works very well though.

In your Home controller add the action Sitemap and the private helper methods. GetRouteUrls is the manually added controller/action routes. GetDynamicUrls builds the data-driven Urls. In my example I have a LiquidsController and a Details(string id) action.

public ActionResult Sitemap()
    {
        var xml = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
               new XElement("urlset",
                   new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
                   , GetRouteUrls()
                   , GetDynamicUrls()
                   )                       
            );                        
        return new XmlActionResult(xml);
    }

    private List<XElement> GetDynamicUrls()
    {
        var result = new List<XElement>();
        using (var db = new ApplicationDbContext())
        {
            var liquids = db.Liquids.ToList();
            foreach (var liquid in liquids)
            {
                result.Add(LocUrl("Liquids", "Details", liquid.FriendlyId));
            }
        }
        return result;
    }

    private List<XElement> GetRouteUrls()
    {
        var result = new List<XElement>();

        result.Add(LocUrl("Account", "Register"));
        result.Add(LocUrl("Account", "Login"));

        result.Add(LocUrl("Home", "Index"));
        result.Add(LocUrl("Home", "About"));
        result.Add(LocUrl("Home", "Contact"));
        result.Add(LocUrl("Home", "TermsOfService"));
        result.Add(LocUrl("Home", "PrivacyStatement"));

        result.Add(LocUrl("Liquids", "Index"));
        result.Add(LocUrl("Vendors", "Index"));
        result.Add(LocUrl("Hardware", "Index"));
        return result;
    }

    private XElement LocUrl(string controller, string action, string id = null)
    {
        if (!string.IsNullOrEmpty(id))
            action = string.Format("{0}/{1}", action, id);         
        var baseUri = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
        return new XElement("url", 
            new XElement("loc", string.Format("{0}{1}/{2}", baseUri, controller, action))
            );
    }

I then added a route so I could access the sitemap doing /sitemap

routes.MapRoute(name: "sitemap", url: "sitemap", defaults: new {controller = "Home", action = "Sitemap"});

The XmlActionResult return type can be found here: Return XML from a controller's action in as an ActionResult?

Community
  • 1
  • 1
Brett Mathe
  • 6,429
  • 7
  • 23
  • 24