简体   繁体   English

我需要做些什么才能使用C#ASP .NET Web API发送Protobuf序列化数据?

[英]What do I need to do to send out Protobuf serialized Data using C# ASP .NET web api?

I'm new to protobuf and fairly new to asp .net. 我是protobuf的新手,并且是.net的新手。 so I might need help on this. 所以我可能需要帮助。 I have this code snippet for my PersonsController: 我的PersonsController有以下代码片段:

public class PersonController : ApiController
{
    [ProtoContract]
    public class Person
    {
        [ProtoMember(1)]
        public int ID { get; set; }
        [ProtoMember(2)]
        public string First { get; set; }
        [ProtoMember(3)]
        public string Last { get; set; }
    }
    // GET api/values
    public IEnumerable<Person> Get()
    {
        List<Person> myList = new List<Person> { 
            new Person { ID = 0, First = "Judy", Last  = "Lee" },
            new Person { ID = 1, First = "John", Last  = "Doe" },
            new Person { ID = 2, First = "George", Last  = "Poole" },
        };

        return myList;
    }
}

and I'm wondering if that's enough to be able to send out the protobuf data and be consumed by other applications? 我想知道是否足以发送出protobuf数据并被其他应用程序使用?

I'm trying to access it directly in google chrome and all I'm getting is an XML format of the data. 我正在尝试直接在Google chrome中访问它,而我得到的只是数据的XML格式。

 <ArrayOfPersonController.Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SampleSerialize.Controllers"> <PersonController.Person> <First>Judy</First> <ID>0</ID> <Last>Lee</Last> </PersonController.Person> <PersonController.Person> <First>John</First> <ID>1</ID> <Last>Doe</Last> </PersonController.Person> <PersonController.Person> <First>George</First> <ID>2</ID> <Last>Poole</Last> </PersonController.Person> </ArrayOfPersonController.Person> 

How do I know if I'm able to send out the serialized data? 我怎么知道我是否能够发送序列化的数据?

You'll need to do a couple of things: 您需要做几件事:

  1. You need a web client that actually asks for protobuf-serialized content in the request's Accept header . 您需要一个Web客户端,该客户端实际上在请求的Accept 标头中 要求提供protobuf序列化的内容 Chrome doesn't do this--it only asks for things like text/html, image/*, etc... stuff you'd expect a web browser to ask for. Chrome不会执行此操作,它只询问您希望Web浏览器要求的诸如text / html,image / *等内容。 There isn't a standard content type for protobuf, so you can just define your own--many people use application/x-protobuf . protobuf没有标准的内容类型,因此您可以定义自己的内容-许多人都使用application/x-protobuf There are Chrome developer tools like Advanced REST client that let you exercise REST APIs from a browser and set your request headers however you'd like. Chrome浏览器开发人员工具(例如Advanced REST客户端)可让您从浏览器中使用REST API,并根据需要设置请求标头。

  2. On the Web API side, you need to create and register your own media formatter. 在Web API方面,您需要创建并注册自己的媒体格式化程序。 There's a good walkthrough here . 有一个很好的演练在这里 You'll probably derive from BufferedMediaTypeFormatter to do the protobuf (de/)serialization, and you'll want to configure this class to handle application/x-protobuf requests. 您可能会从BufferedMediaTypeFormatter派生来进行protobuf(de /)序列化,并且需要配置此类以处理application/x-protobuf请求。 Then you'll need to register it with the Web API pipeline. 然后,您需要在Web API管道中注册它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中使用HttpClient将对象正确发布到ASP.NET Web API? - How do I correctly POST an object to ASP.NET Web API using HttpClient in C#? 在Visual Studio 2013上发布用c#asp.net创建的网站,我需要做什么? - What do I need to do to publish a website made in c# asp.net on Visual Studio 2013? ASP.NET WEB API我需要异步客户端吗 - ASP.NET WEB API do i need async client 在使用C#protobuf-net生成的Objective-c中反序列化“序列化Protobuf”的最佳方法是什么? - What is the best way to Deserialize “Serialized Protobuf” in objective-c which is generated using C# protobuf-net 我需要使用 C# 和 ASP.Net 构建一个重量非常轻的 Ajax 引擎。 你有什么建议? - I need to build a very light weight Ajax Engine using C# and ASP.Net. What do you suggest? 如何使用 C# 中的 Core API 在 ASP.NET 网页中显示 Dropbox 图像文件 - How do I display Dropbox image files in an ASP.NET web page using Core API in C# 从C#原型到ASP.NET,我需要更改什么? - from C# prototype to ASP.NET, what do i need to change? 我需要使用.NET 4和C#开发什么版本的Windows? - What version of windows do I need to develop with .NET 4 and C#? 如何使用.Net(C#)增加时间发送API推送消息(parse.com) - How do I add time to send API push message (parse.com) using .Net(C#) C# Web API 项目上传什么文件? - What files do I upload in a C# Web API project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM