惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 王伟晔

docker 主从mysql配置 利用asp.net Core开发webapi对接云之家智能审批数据互联控件 Windows 2012安装odoo12 Windows有点腻了?不如试试Ubuntu. 用app.net Core搞掂多国语言网站 重建程序员能力(3)-asp.net MVC框架增加Controller 重建程序员能力(2)-如何使asp.net mvc应用增加js和其他功能 重建程序员能力(1) asp.net mvc 5发布部署遇到403.14 我需要在Web上完成一个图片上传的功能(+2) 我需要在Web上完成一个图片上传的功能后续(+1) 我需要在Web上完成一个图片上传的功能 android-studio-bundle-141.1980579-windows download Site Razor提高WebPage代码的易读性 C# Hello World - 王伟晔 用params关键字增强代码的可读性 陌生的yield关键字 发现Visual Studio隐含的大礼包--漂亮的Visual Studio图像库 职业程序员必须要有的工作态度(之一)
处理范例代码Webapi中的Mongodb的Bson中ObjectId反序列化异常
王伟晔 · 2018-12-03 · via 博客园 - 王伟晔

微软代码范例中的一个Bug 处理Mongodb的Bson中ObjectId反序列化异常

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-2.2&tabs=visual-studio-code

Dotnet core 2.1已推出有一段时间,asp.net提供了一段关于Webapi的范例,是使用Dotnet Core+Mongodb搭建Webapi,范例说可以完成Mongodb数据CRUD操作。 一切顺利,按照范例搭建完成,验证创建、查询、删除操作都正常,Update不正常。 报的是ObjectId类型转换异常,直接将Get方法返回的数据贴回去都会报这个异常。 由于Dotnet core 还是新事物,网上资料少,没有找到有价值的方案,所以打算自己尝试。

由于是对象Id序列化的问题:

1、客户端,Update的时候json不传入Id,没有报类型转换异常,但Mongodb报错。

2、所以想着直接用Url传Id进入方法。(可行)

3、修改BookService在更新Mongodb的之前,将Id转换为ObjectId然后不知到对象再更新,book.Id=new ObjectId(string);(可行) 到这一步已经个可以Update数据到Mongodb了,但是想将事情做得完美一点,毕竟传输的Json没有ID,总觉得遗漏了什么。

4、改造Model,将ObjectId的属性增加一个标签[JsonIgnore].(传到客户端的Id都没有了) 5、Model增加一个xxxId getter  setter

 1 [BsonIgnore]
 2 public string BookId {   
 3   get{    
 4       return Id.ToString(); 
 5     }
 6 
 7  set{   
 8    Id=new ObjectId(value); 
 9  } 
10 }

OK. 完美。

然后,在百度上 以BsonIgnore关键字搜索一下,原来还有很多人处理按这种方法处理过同样的问题。