.net-core - 在 .NET Core 3.1 中读取 RSS 不起作用
问题描述
我有非常简单的 .NET Core Web Api 代码,以前在 .NET Core 2.2 中可以使用,但在升级到 3.1 后停止工作
[HttpGet]
[Route("api/news")]
public IEnumerable<SyndicationItem> GetNews(string source)
{
var feed = SyndicationFeed.Load(XmlReader.Create(source));
return feed.Items;
}
我可以通过导航到:https://localhost:5001/api/news?source=http://feeds.bbci.co.uk/news/rss.xml来调用它
在调试中,我可以看到它可以很好地获取项目,但是返回它们时抛出此错误,我不明白为什么在升级到 3.1 后它停止工作以及如何修复它
请指教,谢谢!
System.NotSupportedException:不支持“System.ServiceModel.Syndication.SyndicationItem.AttributeExtensions”上的集合类型“System.Collections.Generic.Dictionary`2[System.Xml.XmlQualifiedName,System.String]”。在 System.Text.Json.JsonClassInfo.GetElementType(类型 propertyType,类型 parentType,MemberInfo memberInfo,JsonSerializerOptions 选项)在 System.Text.Json.JsonClassInfo.CreateProperty(类型声明PropertyType,类型 runtimePropertyType,类型已实现PropertyType,PropertyInfo propertyInfo,类型 parentClassType,JsonConverter转换器, JsonSerializerOptions 选项) 在 System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) 在 System.Text.Json.JsonClassInfo..ctor(Type type,
解决方案
.NET Core 3.x 中的默认JSON 序列化程序更改为System.Text.Json
. 在这种情况下,您可能需要指定Newtonsoft.Json
用作默认序列化程序(旧版本 .NET Core 中的默认序列化程序)。
在Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
// other settings in ConfigureServices...
}
推荐阅读
- html - 将鼠标悬停在文本和叠加转换上
- amazon-web-services - 从不同账户部署 AWS Lambda
- swift - 快速标记数组的自定义信息视图
- react-final-form - react-final-form 字段的动态名称
- python - 在具有 <= 1 MB RAM 和 <= 10 MB ROM 的设备上运行支持向量机内核是否可行?
- angular - Angular 6 ng2 文件上传无法将队列的属性读取为未定义
- powershell - win_shell 完成命令但未结束进程
- mysql - MySQL:一个简单的正则表达式没有按预期工作
- ios - 使用缓存的 UICollectionViewLayoutAttributes 子类化 UICollectionViewLayout
- c# - 使用指向 Func<> 的指针的分配方法