如何在.net core 2中动态创建sitemap.xml?

时间:2018-05-12 22:42:34

标签: asp.net asp.net-core sitemap .net-core-2.0

任何人都可以告诉我如何在.net core 2中创建站点地图? 这article没有在核心2中工作。

11 个答案:

答案 0 :(得分:7)

实际上,我更喜欢使用Razor将其写入模板文件中。假设只有一页,.NET Core 3.1中的示例代码将如下所示(.NET Core 2代码不会有太大不同):

<!-- XmlSitemap.cshtml -->
@page "/sitemap.xml"
@using Microsoft.AspNetCore.Http
@{
    var pages = new List<dynamic>
    {
        new { Url = "http://example.com/", LastUpdated = DateTime.Now }
    };
    Layout = null;
    Response.ContentType = "text/xml";
    await Response.WriteAsync("<?xml version='1.0' encoding='UTF-8' ?>");
}

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach (var page in pages)
    {
        <url>
            <loc>@page.Url</loc>
            <lastmod>@page.LastUpdated.ToString("yyyy-MM-dd")</lastmod>
        </url>
    }
</urlset>

希望这会有所帮助!

答案 1 :(得分:4)

中间件工作正常,但需要一个小修复。

if (context.Request.Path.Value.Equals("/sitemap.xml", StringComparison.OrdinalIgnoreCase))
{
    // Implementation
}
else
    await _next(context);

我创建了一个新项目,然后在添加中间件并运行后,我在浏览器中输入了http://localhost:64522/sitemap.xml,得到了以下结果:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://localhost:64522/home/index</loc>
    <lastmod>2018-05-13</lastmod>
  </url>
  <url>
    <loc>http://localhost:64522/home/about</loc>
    <lastmod>2018-05-13</lastmod>
  </url>
  <url>
    <loc>http://localhost:64522/home/contact</loc>
    <lastmod>2018-05-13</lastmod>
  </url>
  <url>
    <loc>http://localhost:64522/home/privacy</loc>
    <lastmod>2018-05-13</lastmod>
  </url>
  <url>
    <loc>http://localhost:64522/home/error</loc>
    <lastmod>2018-05-13</lastmod>
  </url>
</urlset>

答案 2 :(得分:3)

我从我正在研究的示例Web应用程序中找到了您问题的解决方案。功劳归于Mads Kristensen。这是您正在寻找的非常简化的版本。将此代码与添加操作方法的方式相同,放入HomeController之类的控制器类中。

这是返回XML的方法:

[Route("/sitemap.xml")]
public void SitemapXml()
{
     string host = Request.Scheme + "://" + Request.Host;

     Response.ContentType = "application/xml";

     using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings { Indent = true }))
     {
          xml.WriteStartDocument();
          xml.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

          xml.WriteStartElement("url");
          xml.WriteElementString("loc", host);
          xml.WriteEndElement();

          xml.WriteEndElement();
     }
}

当您输入http://www.example.com/sitemap.xml时,将产生以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     <url>
          <loc>http://www.example.com/</loc>
     </url>
</urlset>

我希望这会有所帮助吗?如果您还发现一些问题,请发布您的解决方案以更新问题。

答案 3 :(得分:1)

幸运的是,已经有一个预先构建的库列表。安装此工具https://github.com/uhaciogullari/SimpleMvcSitemap

然后创建一个像这样的新控制器(github上有更多示例):

public class SitemapController : Controller
{
    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>
        {
            new SitemapNode(Url.Action("Index","Home")),
            new SitemapNode(Url.Action("About","Home")),
            //other nodes
        };

        return new SitemapProvider().CreateSitemap(new SitemapModel(nodes));
    }
}

答案 4 :(得分:1)

博客部分和24小时缓存的动态站点地图“ sitemap-blog.xml”。 (ASP.NET Core 3.1)

  • sitemap.xml 存在于wwwroot中(由xml-sitemaps.com或...生成)。
  • sitemap-blog.xml 动态生成。

robots.txt

User-agent: *
Disallow: /Admin/
Disallow: /Identity/
Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/sitemap-blog.xml

Startup.cs

services.AddMemoryCache();

HomeController.cs

namespace MT.Controllers
{
    public class HomeController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly IMemoryCache _cache;

        public HomeController(
            ApplicationDbContext context,
            IMemoryCache cache)
        {
            _context = context;
            _cache = cache;

        }

        [Route("/sitemap-blog.xml")]
        public async Task<IActionResult> SitemapBlog()
        {
            string baseUrl = $"{Request.Scheme}://{Request.Host}{Request.PathBase}";
            string segment = "blog";
            string contentType = "application/xml";

            string cacheKey = "sitemap-blog.xml";

            // For showing in browser (Without download)
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = cacheKey,
                Inline = true,
            };
            Response.Headers.Append("Content-Disposition", cd.ToString());

            // Cache
            var bytes = _cache.Get<byte[]>(cacheKey);
            if (bytes != null)
                return File(bytes, contentType);

            var blogs = await _context.Blogs.ToListAsync();

            var sb = new StringBuilder();
            sb.AppendLine($"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine($"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"");
            sb.AppendLine($"   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            sb.AppendLine($"   xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");

            foreach (var m in blogs)
            {
                var dt = m.LastModified;
                string lastmod = $"{dt.Year}-{dt.Month.ToString("00")}-{dt.Day.ToString("00")}";

                sb.AppendLine($"    <url>");

                sb.AppendLine($"        <loc>{baseUrl}/{segment}/{m.Slug}</loc>");
                sb.AppendLine($"        <lastmod>{lastmod}</lastmod>");
                sb.AppendLine($"        <changefreq>daily</changefreq>");
                sb.AppendLine($"        <priority>0.8</priority>");

                sb.AppendLine($"    </url>");
            }

            sb.AppendLine($"</urlset>");

            bytes = Encoding.UTF8.GetBytes(sb.ToString());

            _cache.Set(cacheKey, bytes, TimeSpan.FromHours(24));
            return File(bytes, contentType);
        }
    }
}

答案 5 :(得分:1)

以下代码适用于ASP.NET Core 2.2。

 public class SitemapUrl
    {
        public string Page { get; set; }

        public DateTime? LastModifyed { get; set; }

        /*
            always
            hourly
            daily
            weekly
            monthly
            yearly
            never
        */
        public string ChangeFreq { get; set; }

        public float Priority { get; set; } = 0.5f;
    }

    public class SitemapResult : ActionResult
    {
        private readonly IEnumerable<SitemapUrl> _urls;

        public SitemapResult(IEnumerable<SitemapUrl> urls)
        {
            _urls = urls;
        }

        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var response = context.HttpContext.Response;
            response.ContentType = "application/xml; charset=utf-8";

            var settings = new XmlWriterSettings() { Async = true, Encoding = Encoding.UTF8, Indent = false };
            using (var writer = XmlWriter.Create(response.Body, settings))
            {
                WriteToXML(writer);
                await writer.FlushAsync();
            }
        }

        private void WriteToXML(XmlWriter writer)
        {
            writer.WriteStartDocument();
            // Write the urlset.
            writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
            // url element
            foreach (var item in _urls)
            {
                writer.WriteStartElement("url");
                // loc
                writer.WriteStartElement("loc");
                writer.WriteValue(item.Page);
                writer.WriteEndElement();
                // changefreq
                if (!string.IsNullOrEmpty(item.ChangeFreq))
                {
                    writer.WriteStartElement("changefreq");
                    writer.WriteValue(item.ChangeFreq);
                    writer.WriteEndElement();
                }
                // lastmod
                if (item.LastModifyed.HasValue)
                {
                    writer.WriteStartElement("lastmod");
                    writer.WriteValue(item.LastModifyed.Value.ToString("yyyy-MM-dd"));
                    writer.WriteEndElement();
                }

                // priority
                writer.WriteStartElement("priority");
                writer.WriteValue(item.Priority);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
    }

然后在MVC的Controller类中调用SitemapResult

public IActionResult Sitemap(){
    return new SitemapResult(new SitemapUrl[] { new SitemapUrl() { } });
}

在ASP.NET 3.0+中,只需删除asyncAllowSynchronousIO disabled in all serversXmlWriterSettings操作

我使用SitemapHub站点地图工具为多个网站构建XML站点地图,而无需编写代码,从而节省了时间。

答案 6 :(得分:0)

  1. 方法:在ASP.net核心2.2剃刀页面中动态创建sitemap.xml
  2. 概念:使用IPageRouteModelConvention
  3. Nuget:Package Url
  4. 源代码:Github Url

答案 7 :(得分:0)

让我们采用两种方法来获得所需的结果。

首先,这些文章使用中间件方法。使用控制器和SimpleMvcSitemap nuget package

可以轻松解决问题
public class SitemapController : Controller
{
    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>
        {
            new SitemapNode(Url.Action("Index","Home")),
            new SitemapNode(Url.Action("About","Home")),
            //other nodes
        };

        return new SitemapProvider().CreateSitemap(new SitemapModel(nodes));
    }
}

第二部分是使用反射来动态获取所有控制器和动作。以iaspnetcore为例,这里是如何获取控制器和动作列表的方法

            // get available controllers
            var controllers = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => typeof(Controller).IsAssignableFrom(type)
                || type.Name.EndsWith("controller")).ToList();

            foreach (var controller in controllers)
            {
                var controllerName = controller.Name.Replace("Controller", "");

                // get available methods  in controller
                var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                    .Where(method => typeof(IActionResult).IsAssignableFrom(method.ReturnType));
                foreach (var method in methods)
                {
                    var myRoute = Url.Action(method.Name, controllerName);
                }
            }

将所有内容放在一起,我们有这段代码

/// <summary>
/// Base URL Provider for sitemap. Replace with your domain
/// </summary>
public class BaseUrlProvider : IBaseUrlProvider
{
    public Uri BaseUrl => new Uri("https://example.com");
}

public class SitemapController : Controller
{

    [Route("sitemap.xml")]
    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>();


        // get available contrtollers
        var controllers = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => typeof(Controller).IsAssignableFrom(type)
                || type.Name.EndsWith("controller")).ToList();

        foreach (var controller in controllers)
        {
            // get available methods
            var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                .Where(method => typeof(IActionResult).IsAssignableFrom(method.ReturnType));

            foreach (var method in methods)
            {
                // add route name in sitemap
                nodes.Add(new SitemapNode(Url.Action(method.Name, controllerName)));
            }
        }

        return new SitemapProvider(new BaseUrlProvider()).CreateSitemap(new SitemapModel(nodes));
    }
}

使用列表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using SimpleMvcSitemap;
using SimpleMvcSitemap.Routing;

最后只打开路线,例如:

https://localhost:44312/sitemap.xml

答案 8 :(得分:0)

如果您使用的是.net core 2及更高版本,请执行以下操作:

将此添加到.csproj文件

然后在Program.cs文件中添加引用 使用X.Web.Sitemap;

在Program.cs文件中的Main方法内进行此操作:

var sitemap = new Sitemap();

               sitemap.Add(new Url
               {
                    ChangeFrequency = ChangeFrequency.Daily,
                    Location = "https://www.website.com",
                    Priority = 0.5,
                    TimeStamp = DateTime.Now
               });

               sitemap.Add(CreateUrl("https://www.website.com/about"));
               sitemap.Add(CreateUrl("https://www.website.com/services"));
               sitemap.Add(CreateUrl("https://www.website.com/products"));
               sitemap.Add(CreateUrl("https://www.website.com/casestudies"));
               sitemap.Add(CreateUrl("https://www.website.com/blogs"));
               sitemap.Add(CreateUrl("https://www.website.com/contact"));

               //Save sitemap structure to file
               sitemap.Save(@"wwwroot\sitemap.xml");

               //Split a large list into pieces and store in a directory
               //sitemap.SaveToDirectory(@"d:\www\summituniversity.edu.ng\sitemaps");

               //Get xml-content of file
               Console.Write(sitemap.ToXml());
               Console.ReadKey();

主要方法之外:

private static Url CreateUrl(string url)
          {
               return new Url
               {
                    ChangeFrequency = ChangeFrequency.Daily,
                    Location = url,
                    Priority = 0.5,
                    TimeStamp = DateTime.Now
               };
          }

答案 9 :(得分:0)

到目前为止,我在 .Net Core 3.x 中发现的最优雅的方法是使用 ParkSquare.AspNetCore.Sitemap。这会根据您定义的路由动态创建一个 sitemap.xml 和 robots.txt。

在startup.cs中,注册中间件:

app.UseSitemap();

要排除任何内容,您可以修饰控制器类以排除该控制器中的所有内容或特定路由:

// All routes in this controller will be ignored
[SitemapExclude]
public class BlahController : Controller
{
    [Route("some-route")]
    public IActionResult Something()
    {
        return View();
    }
}

public class BlahController : Controller
{
    [SitemapExclude]
    [Route("some-route")]
    public IActionResult Ignored()
    {
        return View();
    }

    [Route("some-other-route")]
    public IActionResult NotIgnored()
    {  
        return View();
    }
}

答案 10 :(得分:0)

启动

 services.AddMvcCore(options =>
        {
            options.OutputFormatters.Clear(); // Remove json for simplicity
            options.OutputFormatters.Add(new MyCustomXmlSerializerOutputFormatter());
        });
app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
       name: "sitemapxml",
       pattern: "/sitemap.xml",
       defaults: new { controller = "Home", action = "SitemapXML" }
       );

自定义格式化程序

  public class MyCustomXmlSerializerOutputFormatter : XmlSerializerOutputFormatter
{
    protected override void Serialize(XmlSerializer xmlSerializer, XmlWriter xmlWriter, object value)
    {

        xmlSerializer = new XmlSerializer(typeof(List<url>), new XmlRootAttribute("urlset"));

        xmlSerializer.Serialize(xmlWriter, value);
    }
}

网址类

public class url
{
    

    public string changefreq { get; set; }
    public DateTime? lastModified { get; set; }
    public double? priority { get; set; }
    public string loc { get; set; }
}

控制器

    [HttpGet]
    [Produces("application/xml")]
    public ActionResult<List<url>> SitemapXML() 
    {
        var list = new List<url>();

        var dokumanlar = _dokumanCategoryService.GetAll().Where(i => i.Yatirimci == 1).ToList();


        

        foreach (var dokuman in dokumanlar)
        {
            var dokumanurl = dokuman.SeoUrl;

            var culture = dokuman.Culture;

            if (dokuman.Culture == "tr")
            {
                list.Add(new url { lastModified = DateTime.UtcNow, priority = 0.8, loc = $"https://example.com/yatirimci-iliskileri/{dokumanurl}", changefreq = "always" });
            }
            else
            {
                list.Add(new url { lastModified = DateTime.UtcNow, priority = 0.8, loc = $"https://example.com/{culture}/investor-relations/{dokumanurl}", changefreq = "always" });
            }


        }


        

        return list;
    }