views:

121

answers:

2

Hi,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.

A: 

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

Charlino
Thanks, that will be useful
ryudice
A: 

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

    1     /// <summary>
    2     /// This attribute indicates that a method is an actual page and gives the data for it
    3     /// </summary>
    4     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    5     public class MVCUrlAttribute : ActionFilterAttribute
    6     {
    7         public string Url { get; private set; }
    8 
    9         public MVCUrlAttribute(string url)
   10         {
   11             this.Url = url;
   12         }
   13 
   14         public override void OnResultExecuting(ResultExecutingContext filterContext)
   15         {
   16             string fullyQualifiedUrl = filterContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + this.Url;
   17             // We build HTML here because we want the View to be easily able to include it without any conditionals
   18             // and because the ASP.NET WebForms view engine sometimes doesn’t subsitute <% in certain head items
   19             filterContext.Controller.ViewData["CanonicalUrl"] = @”<link rel=”"canonical”" href=”"” + fullyQualifiedUrl + ” />”;
   20             base.OnResultExecuting(filterContext);
   21         }
   22     }

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

    1             List<string> allPageUrls = new List<string>();
    2 
    3             // Find all the MVC Routes
    4             Log.Debug(“*** FINDING ALL MVC ROUTES MARKED FOR INCLUSION IN SITEMAP”);
    5             var allControllers = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)));
    6             Log.DebugFormat(“Found {0} controllers”, allControllers.Count());
    7 
    8             foreach (var controllerType in allControllers)
    9             {
   10                 var allPublicMethodsOnController = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
   11                 Log.DebugFormat(“Found {0} public methods on {1}”, allPublicMethodsOnController.Count(), controllerType.Name);
   12 
   13                 foreach (var publicMethod in allPublicMethodsOnController)
   14                 {
   15                     var mvcurlattr = publicMethod.GetCustomAttributes(true).OfType<MVCUrlAttribute>().FirstOrDefault();
   16                     if (mvcurlattr != null)
   17                     {
   18                         string url = mvcurlattr.Url;
   19                         Log.Debug(“Found “ + controllerType.Name + “.” + publicMethod.Name + ” <– “ + url);
   20                         allPageUrls.Add(url);
   21                     }
   22                 }
   23             }

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/

Hightechrider