コード例 #1
0
        public virtual async Task <ActionResult> Index()
        {
            var items         = new List <SitemapItem>();
            var languagesList = _languagesService.GetAsEnumerable().Where(p => p.Published)
                                .OrderByDescending(p => p.IsDefault);

            foreach (var language in languagesList)
            {
                items.Add(new SitemapItem(
                              Url.Action("Index", "Home",
                                         new { lang = language.IsoCode },
                                         Request.Url.Scheme), DateTime.Now,
                              SitemapChangeFrequency.Daily, 1));

                items.AddRange((await _pagesService.GetAsEnumerableAsync()).Where(p => p.Published).Select(page =>
                                                                                                           new SitemapItem(
                                                                                                               Url.Action("Index", "Page", new { slug = page.Slug, lang = language.IsoCode },
                                                                                                                          Request.Url.Scheme), DateTime.Now,
                                                                                                               SitemapChangeFrequency.Daily, 1)));

                items.AddRange((await _tagsService.GetAsEnumerableAsync()).Select(tag =>
                                                                                  new SitemapItem(
                                                                                      Url.Action("Tag", "Search",
                                                                                                 new { tag = tag.GetLocalized(x => x.Tag, language.Id), lang = language.IsoCode },
                                                                                                 Request.Url.Scheme), DateTime.Now,
                                                                                      SitemapChangeFrequency.Daily, 0.9)).DistinctBy(p => p.Url));


                items.AddRange((await _categoriesService.GetAsEnumerableAsync()).Select(category =>
                                                                                        new SitemapItem(
                                                                                            Url.Action("FilterByCategory", "Product", new { slug = category.Slug, lang = language.IsoCode },
                                                                                                       Request.Url.Scheme), DateTime.Now,
                                                                                            SitemapChangeFrequency.Daily, 0.8)));

                items.AddRange((await _categoriesService.GetAsEnumerableAsync()).Select(category =>
                                                                                        new SitemapItem(
                                                                                            Url.Action("FilterByCategory", "Blog", new { slug = category.Slug, lang = language.IsoCode },
                                                                                                       Request.Url.Scheme), DateTime.Now,
                                                                                            SitemapChangeFrequency.Daily, 0.8)));


                foreach (var post in _postService.GetNewItemsForSiteMap())
                {
                    Uri url = new Uri(Url.Action("Index", "Search", new
                    {
                        lang        = language.IsoCode,
                        OrderBy     = SearchResultSortType.Score,
                        SearchPlace = SearchPlace.Title,
                        Query       = post.Title
                    }, Request.Url.Scheme));

                    if (post.PostType == PostType.BlogPost)
                    {
                        url = new Uri(Url.Action("Post", "Blog", new { slug = post.Slug, lang = language.IsoCode }, Request.Url.Scheme));
                    }
                    if (post.PostType == PostType.Product)
                    {
                        url = new Uri(Url.Action("Index", "Product", new { slug = post.Slug, lang = language.IsoCode }, Request.Url.Scheme));
                    }

                    items.Add(new SitemapItem(url.ToString(), post.LastUpDate ?? post.PublishDate,
                                              SitemapChangeFrequency.Weekly, 0.7));
                }
            }

            return(Content(_sitemapGenerator.GenerateSiteMap(items).ToString(), "text/xml", Encoding.UTF8));
        }