我是protobuf的新手,也是asp .net的新手。所以我可能需要帮助。我有我的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;
}
}
我想知道这是否足以能够发送protobuf数据并被其他应用程序使用?
我正试图直接在谷歌浏览器中访问它,而我所得到的只是数据的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>
我如何知道我是否能够发送序列化数据?
答案 0 :(得分:2)
你需要做几件事:
您需要一个实际上在请求Accept
header中请求protobuf序列化内容的网络客户端。 Chrome不会这样做 - 它只会询问文本/ html,图片/ *等等...您希望网络浏览器要求的东西。 protobuf没有标准内容类型,因此您可以定义自己的内容类型 - 许多人使用application/x-protobuf
。有一些Chrome开发人员工具,例如Advanced REST client,可以让您从浏览器中运行REST API并设置您喜欢的请求标题。
在Web API方面,您需要创建并注册自己的媒体格式化程序。这是一个很好的演练here。您可能会从BufferedMediaTypeFormatter派生出来进行protobuf(de /)序列化,并且您希望配置此类来处理application/x-protobuf
个请求。然后,您需要使用Web API管道注册它。