Question

would it be possible to generate a XML sitemap for search engines automatically by iterating the routes and the controllers' action of an applicacion? If you could provide me with an idea or so I would appreciate it. Thanks.

Was it helpful?

Solution

Have a gander at ASP.NET MVC SiteMap provider - MvcSiteMap.

I haven't used it myself so I can't vouch for it, but it did spring to mind when I read your question.

HTHs,
Charles

OTHER TIPS

Create an attribute like this and apply it to any methods that you want as pages in sitemap.xml file.

/// <summary>
/// This attribute indicates that a method is an actual page and gives the data for it
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MVCUrlAttribute : ActionFilterAttribute
{
    public string Url { get; private set; }

    public MVCUrlAttribute(string url)
    {
        this.Url = url;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        string fullyQualifiedUrl = filterContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + this.Url;
        // We build HTML here because we want the View to be easily able to include it without any conditionals
        // and because the ASP.NET WebForms view engine sometimes doesn’t subsitute <% in certain head items
        filterContext.Controller.ViewData["CanonicalUrl"] = @”<link rel=”"canonical”" href=”"” + fullyQualifiedUrl + ” />”;
        base.OnResultExecuting(filterContext);
    }
}

Now use reflection to find all of those 'pages':-

 List<string> allPageUrls = new List<string>();

 // Find all the MVC Routes
 Log.Debug(“*** FINDING ALL MVC ROUTES MARKED FOR INCLUSION IN SITEMAP”);
 var allControllers = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)));
 Log.DebugFormat(“Found {0} controllers”, allControllers.Count());

 foreach (var controllerType in allControllers)
 {
     var allPublicMethodsOnController = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
     Log.DebugFormat(“Found {0} public methods on {1}”, allPublicMethodsOnController.Count(), controllerType.Name);

     foreach (var publicMethod in allPublicMethodsOnController)
     {
         var mvcurlattr = publicMethod.GetCustomAttributes(true).OfType<MVCUrlAttribute>().FirstOrDefault();
         if (mvcurlattr != null)
         {
             string url = mvcurlattr.Url;
             Log.Debug(“Found “ + controllerType.Name + “.” + publicMethod.Name + ” <– “ + url);
             allPageUrls.Add(url);
         }
     }
 }

Now build the sitemap.xml file from this list.

Note also that the ActionFilter puts a canonical URL into your view model so you can easily make all your pages search engine friendly even if you have duplicate content.

You CANNOT do this from routes because you might have just one route for your entire application {controller}/{action}.

More here:- http://blog.abodit.com/2010/02/sitemap-xml-asp-net-aspnet-mvc/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top