.net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)

.net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)

首先设置 Startup.cs 文件

  • 配置 ConfigureServices
            services
                .AddMvc(options =>
                {
                    options.RespectBrowserAcceptHeader = true;
                    options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
                    options.FormatterMappings.SetMediaTypeMappingForFormat("xml", Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/xml"));
                    options.FormatterMappings.SetMediaTypeMappingForFormat("config", Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/xml"));
                    options.FormatterMappings.SetMediaTypeMappingForFormat("js", Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"));
                })
                .AddXmlSerializerFormatters()
                .AddDataAnnotationsLocalization()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

例子:生成sitemap.xml

  • 返回值 Urlset
using System.Collections.Generic;
using System.Xml.Serialization;

namespace MyFramework.Services.Sitemaps.Dtos
{
    [XmlRoot(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
    [XmlType("urlset")]
    public class Urlset
    {
        [XmlElement("url")]
        public List Urls { get; set; }
    }
}
using System.Xml.Serialization;

namespace MyFramework.Services.Sitemaps.Dtos
{
    [XmlType("url")]
    public class Url
    {
        public string loc { get; set; }
        public string priority { get; set; }
        public string lastmod { get; set; }
        public string changefreq { get; set; }
    }
}
  • GetUrlset 方法
using MyFramework.Services.Sitemaps.Dtos;
using System;
using System.Collections.Generic;

namespace MyFramework.Services.Sitemaps
{
    public class SitemapService : ServiceBaseModule, ISitemapService
    {
        public Urlset GetUrlset()
        {
            var __baseUrl = new List
            {
                new Url{ loc="http://www.mywebsite.com",priority="1.00",lastmod= DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),changefreq="weekly"},
                new Url{ loc="http://www.mywebsite.com/Home",priority="1.00",lastmod= DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),changefreq="weekly"},
                new Url{ loc="http://www.mywebsite.com/Home/Index.html",priority="1.00",lastmod= DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),changefreq="weekly"},
            };

            for (var i = 0; i < 10; i++)
            {
                __baseUrl.Add(new Url { loc = "http://www.mywebsite.com/news/news-" + i + ".html", priority = "1.00", lastmod = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), changefreq = "daily" });
            }

            var __urlset = new Urlset
            {
                Urls = __baseUrl
            };
            return __urlset;
        }
    }
}
  • SitemapController Controller
using Microsoft.AspNetCore.Mvc;
using MyFramework.Services.Sitemaps;
using MyFramework.Services.Sitemaps.Dtos;

namespace MyFramework.Web.Api.Controllers
{
    [Route("")]
    [ApiController]
    public class SitemapController : ControllerBase
    {
        #region Initialize
        private readonly ISitemapService _sitemapService;
        public SitemapController(ISitemapService sitemapService)
        {
            _sitemapService = sitemapService;
        }
        #endregion

        #region Methods
        [HttpGet("sitemap.{format}"), FormatFilter]
        public Urlset GetSitemap()
        {
            return _sitemapService.GetUrlset();
        }
        #endregion
    }
}

最终效果如图

.net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)_第1张图片

你可能感兴趣的:(.net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml))