From 23a9399b43ebce1ecc22d96a5311163955fde622 Mon Sep 17 00:00:00 2001 From: "DESKTOP-M4V1LH5\\Jin" Date: Mon, 11 May 2020 19:14:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sitemap.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COMCMS.Web/Controllers/HomeController.cs | 59 ++++++++++++------- COMCMS.Web/Views/Home/SitemapHTML.cshtml | 14 +++++ COMCMS.Web/Views/Home/SitemapXML.cshtml | 32 ++++++++++ COMCMS.Web/Views/Shared/_SitemapLayout.cshtml | 4 ++ 4 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 COMCMS.Web/Views/Home/SitemapHTML.cshtml create mode 100644 COMCMS.Web/Views/Home/SitemapXML.cshtml create mode 100644 COMCMS.Web/Views/Shared/_SitemapLayout.cshtml diff --git a/COMCMS.Web/Controllers/HomeController.cs b/COMCMS.Web/Controllers/HomeController.cs index 91acde0..5f0561b 100644 --- a/COMCMS.Web/Controllers/HomeController.cs +++ b/COMCMS.Web/Controllers/HomeController.cs @@ -186,27 +186,27 @@ namespace COMCMS.Web.Controllers #region 测试 //[Route("c/{*path}/index2.html")] - public IActionResult Test(string path = "") - { - var asms = AppDomain.CurrentDomain.GetAssemblies(); - List list = new List(); - foreach (var item in asms) - { - string fullName = item.GetName().ToString(); - if (fullName.IndexOf("COMCMS.Web.Views")>-1) - { - var types = item.GetTypes().Where(e => e.Name.StartsWith("Views_Article")).ToList(); - foreach (var type in types) - { - string viewName = type.Name.Replace("Views_Article_", "") + ".cshtml"; - list.Add(viewName); - } + //public IActionResult Test(string path = "") + //{ + // var asms = AppDomain.CurrentDomain.GetAssemblies(); + // List list = new List(); + // foreach (var item in asms) + // { + // string fullName = item.GetName().ToString(); + // if (fullName.IndexOf("COMCMS.Web.Views")>-1) + // { + // var types = item.GetTypes().Where(e => e.Name.StartsWith("Views_Article")).ToList(); + // foreach (var type in types) + // { + // string viewName = type.Name.Replace("Views_Article_", "") + ".cshtml"; + // list.Add(viewName); + // } - } - //list.Add(item.GetName().ToString()); - } - return Content(JsonConvert.SerializeObject(list)); - } + // } + // //list.Add(item.GetName().ToString()); + // } + // return Content(JsonConvert.SerializeObject(list)); + //} //[Route("{path1:regex([[a-zA-Z0-9-]])}/{path2:regex([[a-zA-Z0-9-]])}/index.html")] //public IActionResult Test2(string path1 = "", string path2 = "") //{ @@ -425,6 +425,25 @@ namespace COMCMS.Web.Controllers } #endregion + #region sitemap html + [Route("sitemap.html")] + [HttpGet] + public IActionResult SitemapHTML() + { + return View(); + } + #endregion + + #region sitemap xml + [HttpGet] + [Route("sitemap.xml")] + public IActionResult SitemapXML() + { + Response.ContentType = "text/xml"; + return View(); + } + #endregion + } } \ No newline at end of file diff --git a/COMCMS.Web/Views/Home/SitemapHTML.cshtml b/COMCMS.Web/Views/Home/SitemapHTML.cshtml new file mode 100644 index 0000000..2c4b85b --- /dev/null +++ b/COMCMS.Web/Views/Home/SitemapHTML.cshtml @@ -0,0 +1,14 @@ +@{ + Layout = null; +} + + + + + + + SitemapHTML + + + + diff --git a/COMCMS.Web/Views/Home/SitemapXML.cshtml b/COMCMS.Web/Views/Home/SitemapXML.cshtml new file mode 100644 index 0000000..ad92a77 --- /dev/null +++ b/COMCMS.Web/Views/Home/SitemapXML.cshtml @@ -0,0 +1,32 @@ +@{ + Layout = "~/Views/Shared/_SitemapLayout.cshtml"; + string url = Utils.GetServerUrl(); + string lastmod = DateTime.Now.ToString("yyyy-MM-dd"); + //所有文章栏目 + IList articleCategories = ArticleCategory.FindAll(ArticleCategory._.IsHide == 0, null, null, 0, 0); + //前2000篇文章 + IList
articles = Article.FindAll(Article._.IsHide == 0, null, null, 0, 2000); +} + + @url + 1.0 + daily + +@foreach (var item in articleCategories) +{ + + @(url)/article/index/@(item.Id) + 0.80 + @(lastmod) + daily + +} +@foreach (var item in articles) +{ + + @(url)/article/detail/@(item.Id) + 0.80 + @(lastmod) + daily + +} \ No newline at end of file diff --git a/COMCMS.Web/Views/Shared/_SitemapLayout.cshtml b/COMCMS.Web/Views/Shared/_SitemapLayout.cshtml new file mode 100644 index 0000000..4642c2e --- /dev/null +++ b/COMCMS.Web/Views/Shared/_SitemapLayout.cshtml @@ -0,0 +1,4 @@ + + + @RenderBody() +