.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<Url> 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<Url>
{
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. sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml

    Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...

  2. 帝国CMS如何自动生成sitemap.xml网站地图文件

    登录网站的后台http://你的域名/e/admin/ 进入后台栏目 =>增加自定义页面 =>选择直接页面,页面名称为:网站地图,文件名修改为  ../../sitemap.xml 内容填 ...

  3. 网站robots.txt & sitemap.xml

    1. 如何查看网站的robots.txt 网址/robots.txt, 比如小米  https://www.mi.com/robots.txt sitemap.xml

  4. AJAX请求返回JSON数据动态生成html

    1:DeliveryPersonVO对象 package com.funcanteen.business.entity.delivery.vo; import java.util.List; impo ...

  5. php动态生成一个xml文件供swf调用

    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdo ...

  6. 根据xml配置使用反射动态生成对象

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  7. 请求*.html后缀无法返回json数据的问题

    在springmvc中请求*.html不可以返回json数据. 修改web.xml,添加url拦截格式.

  8. C# XML与Json之间相互转换实例详解

    对于这转换其实很简单,其中最重要的就是先要引用类库.可以到官网进行下载引用http://json.codeplex.com. XML转换为Json字符串 string xml = @"< ...

  9. WordPress免插件生成完整站点地图(sitemap.xml)的php代码

    让这个代码更加完善,可以同时生成首页.文章.单页面.分类和标签的 sitemap! 一.PHP 代码 <?php require('./wp-blog-header.php'); header( ...

随机推荐

  1. Flask拾遗总汇1

    目录 1.flask的路由分发方式 2.请求响应相关 3.flask配置文件拾遗(config) 4.路由系统参数配置 4.1 可传入参数: 4.2 常用路由系统有以上五 5.反向生成URL: url ...

  2. debian 系统修改密码

    1.在Grub的引导装载程序菜单上,选择你要进入的条目,键入 “e” 来进入编辑模式.2.在第二行(类似于kernel /vmlinuz-2.6.15 ro root=/dev/hda2 ),键入”e ...

  3. DVWA的搭建

    DVWA的搭建 一.DVWA是什么? 一款渗透测试演练系统,俗称靶机. 二.如何搭建? Linux有成套的靶机,直接打开使用就可以,下面开始介绍Windows 下DVWA的搭建. 运行phpstudy ...

  4. xshell 连接报错 Disconnected from remote host

    Type `help' to learn how to use Xshell prompt. [c:\~]$ Connecting to 20.0.0.91:22...Connection estab ...

  5. vue-router编程式跳转

    除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现. [语法] .

  6. nginx docker 命令: command not found

    1. ps: command not found 使用如下命令安装 apt-get update && apt-get -y install procps 2. vim: comman ...

  7. nginx 代理服务

    1.nginx反向代理服务 location ~ /test_proxy.html$ { proxy_pass http://127.0.0.1:8080;(代理访问127.0.0.1:8080) } ...

  8. 如何修改dedecms专题目录默认名称special

    专题有一个聚合的效果,一般会比普通的文章页更符合用户需求.如果用dedecms建专题的话,默认的目录是special,怎么修改修改dedecms专题目录名称呢,比如将/special/改为/s/这样更 ...

  9. NOIP 2003 栈

    洛谷 P1044 栈 洛谷传送门 JDOJ 1291: [NOIP2003]栈 T3 JDOJ传送门 题目描述 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两 ...

  10. flask框架搭建项目的具体步骤

    flask是一个使用python编写的轻量级Web应用框架.与django不同,Django创建工程时,会直接构架好工程目录.而flask工程几乎是自己创建结构. 1.导入相关模块以及需要使用的模块: ...