Dynamic sitemap in ASP.NET MVC

前端 未结 7 819
小蘑菇
小蘑菇 2021-01-30 09:04

I\'m trying to create an automatic sitemap ActionResult that outputs a valid sitemap.xml file. The actual generation of the file is not a problem, but I can\'t seem to figure o

7条回答
  •  悲&欢浪女
    2021-01-30 09:41

    As likwid mentions, you want to reflect upon your model(s) namespace and obtain all classes that implement IController. Once you have the collection, you want to reflect to see what Members (methods) return the type ActionResult.

    Perhaps you can create your own attribute, [SitemapAttribute] that lets you selectively specify what methods to index in the sitemap (i.e., Index(), but not Edit()). Yeah, I like that idea of controlling which methods (urls) gets written.

    This is an excellent question because I was just thinking of doing the same. +1!

    // Controller abstract implements IController
    public class HelpController : Controller
    {
      public HelpController()
      {
      }
    
      [Sitemap]
      public ActionResult Index()
      {
        // does get written to the file, cause of [Sitemap]
      }
    
      public ActionResult Create()
      {
        // does not get mapped to the file
      }
    
      public ActionResult Edit()
      {
        // does not get mapped to the file
      }
    
      [Sitemap]
      public ActionResult ViewArticle()
      {
        // would get indexed.
      }
    }
    

    For how to do reflection, here's a good MSDN article to get you introduced to reflection:

    http://msdn.microsoft.com/en-us/library/ms172331.aspx

    Good question!

提交回复
热议问题