如何在我的 ASP.NET Core Web API 中启用 BSON 序列化?
How do I enable BSON serialization in my ASP.NET Core Web API?
我是 ASP.NET 核心和网络编程的新手。我刚刚根据 RESTfull 设计原则成功完成了我的第一个 ASP.NET Core Web API。它目前正在使用 JSON 序列化来发送响应(默认为 Visual Studio),但我想尝试 BSON。我花了一天时间在谷歌上搜索,但似乎找不到任何有关如何向服务器添加 BSON serialization/deserialization 功能的示例。我看过几篇关于如何在全框架 ASP.NET 上执行此操作的文章,其中它已经开箱即用好几年了(例如:https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/bson-support-in-web-api-21 and http://www.strathweb.com/2012/07/bson-binary-json-and-how-your-web-api-can-be-even-faster/),但与ASP.NET 核心。
我搜索了 VS 生成的源代码文件,希望找到与我 linked 的完整框架示例类似的内容,但由于相似之处很少,所以我什么也没想到。有人可以请post(或link)一些代码来说明在ASP.NET Core 中是如何完成的吗?非常感谢你提前。
您可以使用此格式化程序:https://github.com/WebApiContrib/WebAPIContrib.Core
然后在 startup.cs 添加:
using WebApiContrib.Core.Formatter.Bson;
namespace MyApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddBsonSerializerFormatters();
}
}
}
我是 ASP.NET 核心和网络编程的新手。我刚刚根据 RESTfull 设计原则成功完成了我的第一个 ASP.NET Core Web API。它目前正在使用 JSON 序列化来发送响应(默认为 Visual Studio),但我想尝试 BSON。我花了一天时间在谷歌上搜索,但似乎找不到任何有关如何向服务器添加 BSON serialization/deserialization 功能的示例。我看过几篇关于如何在全框架 ASP.NET 上执行此操作的文章,其中它已经开箱即用好几年了(例如:https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/bson-support-in-web-api-21 and http://www.strathweb.com/2012/07/bson-binary-json-and-how-your-web-api-can-be-even-faster/),但与ASP.NET 核心。
我搜索了 VS 生成的源代码文件,希望找到与我 linked 的完整框架示例类似的内容,但由于相似之处很少,所以我什么也没想到。有人可以请post(或link)一些代码来说明在ASP.NET Core 中是如何完成的吗?非常感谢你提前。
您可以使用此格式化程序:https://github.com/WebApiContrib/WebAPIContrib.Core
然后在 startup.cs 添加:
using WebApiContrib.Core.Formatter.Bson;
namespace MyApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddBsonSerializerFormatters();
}
}
}