Byndyusoft.AspNetCore.Mvc.Formatters.ProtoBuf
v0.2.0: Merge pull request #3 from Byndyusoft
ASP.NET核心MVC格式用于Protobuf输入和输出。
协议缓冲区是Google的语言中性,平台中性的,可扩展的机制,用于序列化结构化数据 - 想想XML,但更小,更快,更简单。
当客户指定Accept标头时,会发生内容协商。 ASP.NET Core使用的默认格式是JSON。但是客户可以指定ProtoBuf格式:
using ( var httpClient = new HttpClient ( ) )
{
client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/protobuf" ) ) ;
var response = await client . GetAsync ( "/api/products/5" ) ;
var product = response . Content . ReadFromProtoBufAsync < Product > ( ) ;
}在此示例中,使用了byndyusoft.net.http.protobuf软件包。
需要支持ProtoBuf格式的ASP.NET核心应用程序可以添加Byndyusoft.AspNetCore.Mvc.Formatters.ProtoBuf nuget软件包和配置支持。有用于输入和输出的单独的格式。输入格式器由模型结合使用。输出格式用于格式化响应。
dotnet add package Byndyusoft.AspNetCore.Mvc.Formatters.ProtoBuf使用ProtoBufSerializer实施的Protobuf Formatters是通过调用AddProtoBufFormatters配置的:
public void ConfigureServices ( IServiceCollection services )
{
services . AddMvcCore ( )
. AddProtoBufFormatters ( ) ;
}前面的代码使用ProtoBufSerializer序列化结果。
客户可以要求特定格式作为URL的一部分,例如:
请求路径的映射应在API使用的路由中指定。例如:
[ Route ( "api/[controller]" ) ]
[ ApiController ]
[ FormatFilter ]
public class ProductsController : ControllerBase
{
[ HttpGet ( "{id}.{format?}" ) ]
public Product Get ( int id )
{前面的路由允许将请求的格式指定为可选的文件扩展名。 [FormatFilter]属性检查是否存在routedata中格式值的存在,并在创建响应时将响应格式映射到适当的格式。
| 路线 | 格式化 |
|---|---|
| /api/products/5 | 默认输出格式 |
| /PI/products/5.json | JSON Formatter(如果配置) |
| /PI/products/5.protobuf | Protobuf Formatter(如果配置) |
为了做出贡献,您需要设置本地环境,请参阅先决条件。有关贡献和工作流程指南,请参阅包装开发生命周期。
有关如何贡献的详细概述可以在贡献指南中找到。
确保您已经在开发机上安装了以下所有先决条件:
src中实现软件包逻辑tests中添加或添加或附加单位测试(与编码之前和同时使用编码)master分支