http://www.cnblogs.com/r01cn/archive/2013/05/17/3083400.html

6.1 Media Formatters
6.1 媒体格式化器

本文引自:http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

By Mike Wasson|March 8, 2012
作者:Mike Wasson|日期:2012-3-8

This tutorial shows how support additional media formats in ASP.NET Web API.
本教程演示如何在ASP.NET Web API中支持额外的媒体格式。

6.1.1 Internet Media Types
6.1.1 Internet的媒体类型

A media type, also called a MIME type, identifies the format of a piece of data. In HTTP, media types describe the format of the message body. A media type consists of two strings, a type and a subtype. For example:
媒体类型,也叫做MIME类型,标识了一片数据的格式。在HTTP中,媒体类型描述了消息体的格式。一个媒体类型由两个字符串组成:类型和子类型。例如:

  • text/html
  • image/png
  • application/json

When an HTTP message contains an entity-body, the Content-Type header specifies the format of the message body. This tells the receiver how to parse the contents of the message body.
当一条HTTP消息含有一个实体时,Content-Type(内容类型)报头指定消息体的格式。这是告诉接收器如何解析消息体的内容。

For example, if an HTTP response contains a PNG image, the response might have the following headers.
例如,如果一个HTTP响应含有一个PNG图片,该响应可能会有以下报头。

HTTP/1.1 200 OK
Content-Length: 95267
Content-Type: image/png

When the client sends a request message, it can include an Accept header. The Accept header tells the server which media type(s) the client wants from the server. For example:
当客户端发送一条请求消息时,它可能包括一个Accept报头。Accept报头是告诉服务器,客户端希望从服务器得到哪种媒体类型。例如:

Accept: text/html,application/xhtml+xml,application/xml

This header tells the server that the client wants either HTML, XHTML, or XML.
该报头告诉服务器,客户端希望得到的是HTML、XHTML,或XML。

In Web API, the media type determines how Web API serializes and deserializes the HTTP message body. There is built-in support for XML, JSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.
在Web API中,媒体类型决定了Web API如何对HTTP消息体进行序列化和解序列化。对于XML、JSON,以及URL编码的表单数据,已有了内建的支持。而且,通过编写媒体格式化器(Media Formatter),可以支持额外的媒体类型。

To create a media formatter, derive from one of these classes:
为了创建媒体格式化器,需从以下类进行派生:

  • MediaTypeFormatter. This class uses asynchronous read and write methods.
    MediaTypeFormatter。这个类使用了异步读写方法
  • BufferedMediaTypeFormatter. This class derives from MediaTypeFormatter but wraps the asynchronous read/write methods inside synchronous methods.
    BufferedMediaTypeFormatter。这个类派生于MediaTypeFormatter,但将异步读写方法封装在同步方法之中。

Deriving from BufferedMediaTypeFormatter is simpler, because there is no asynchronous code, but it also means the calling thread can block during I/O. 
从BufferedMediaTypeFormatter派生要更简单些,因为没有异步代码,但它也意味着在I/O期间可能会阻塞线程。

6.1.2 Creating a Media Formatter
6.1.2 创建媒体格式化器

The following example shows a media type formatter that can serialize a Product object to a comma-separated values (CSV) format. This example uses the Product type defined in the tutorial Creating a Web API that Supports CRUD Operations. Here is the definition of the Product object:
以下示例演示了一个媒体类型格式化器,它可以将Product对象序列化成一个逗号分隔的值(CSV)格式。该示例使用了“创建支持CRUD操作的Web API”教程(本系列教程的第2.1小节)中定义的Product类型。以下是Product对象的定义:

namespace ProductStore.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}

To implement a CSV formatter, define a class that derives from BufferedMediaTypeFormater:
为了实现CSV格式化器,要定义一个派生于BufferedMediaTypeFormater的类:

namespace ProductStore.Formatters
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using ProductStore.Models;
public class ProductCsvFormatter : BufferedMediaTypeFormatter
{
}
}

In the constructor, add the media types that the formatter supports. In this example, the formatter supports a single media type, "text/csv":
在其构造器中,要添加一个该格式化器所支持的媒体类型。在这个例子中,该格式化器只支持单一的媒体类型:“text/csv”:

public ProductCsvFormatter()
{
// Add the supported media type.
// 添加所支持的媒体类型
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/csv"));
}

Override the CanWriteType method to indicate which types the formatter can serialize:
重写这个CanWriteType方法,以指示该格式化器可以序列化哪种类型:

public override bool CanWriteType(System.Type type)
{
if (type == typeof(Product))
{
return true;
}
else
{
Type enumerableType = typeof(IEnumerable<Product>);
return enumerableType.IsAssignableFrom(type);
}
}

In this example, the formatter can serialize single Product objects as well as collections of Product objects.
在这个例子中,格式化器可以序列化单个Product对象,以及Product对象集合。

Similarly, override the CanReadType method to indicate which types the formatter can deserialize. In this example, the formatter does not support deserialization, so the method simply returns false.
相应地,重写CanReadType方法,以指示该格式化器可以解序列化哪种类型。在此例中,格式化器不支持解序列化,因此该方法简单地返回false。

protected override bool CanReadType(Type type)
{
return false;
}

Finally, override the WriteToStream method. This method serializes a type by writing it to a stream. If your formatter supports deserialization, also override the ReadFromStream method.
最后,重写WriteToStream方法。通过将一种类型写成一个流,该方法对该类型进行序列化。如果你的格式化器要支持解序列化,也可以重写ReadFromStream方法。

public override void WriteToStream(
Type type, object value, Stream stream, HttpContentHeaders contentHeaders)
{
using (var writer = new StreamWriter(stream))
{
var products = value as IEnumerable<Product>;
if (products != null)
{
foreach (var product in products)
{
WriteItem(product, writer);
}
}
else
{
var singleProduct = value as Product;
if (singleProduct == null)
{
throw new InvalidOperationException("Cannot serialize type");
}
WriteItem(singleProduct, writer);
}
}
stream.Close();
}
// Helper methods for serializing Products to CSV format.
// 将Product序列化成CSV格式的辅助器方法
private void WriteItem(Product product, StreamWriter writer)
{
writer.WriteLine("{0},{1},{2},{3}", Escape(product.Id),
Escape(product.Name), Escape(product.Category), Escape(product.Price));
}
static char[] _specialChars = new char[] { ',', '\n', '\r', '"' };
private string Escape(object o)
{
if (o == null)
{
return "";
}
string field = o.ToString();
if (field.IndexOfAny(_specialChars) != -1)
{
return String.Format("\"{0}\"", field.Replace("\"", "\"\""));
}
else return field;
}

6.1.4 Adding the Media Formatter
6.1.4 添加媒体格式化器

To add a media type formatter to the Web API pipeline, use the Formatters property on the HttpConfiguration object.
为了将媒体类型格式化器添加到Web API管线,要使用HttpConfiguration对象上的Formatters属性。

public static void ConfigureApis(HttpConfiguration config)
{
config.Formatters.Add(new ProductCsvFormatter());
}

For ASP.NET hosting, add this function to the Global.asax file and call it from the Application_Start method.
对于ASP.NET托管,要将这个函数添加到Global.asax文件,并通过Application_Start方法调用它。

protected void Application_Start()
{
ConfigureApis(GlobalConfiguration.Configuration);
// ...
}

Now if a client specifies "text/csv" in the Accept header, the server will return the data in CSV format.
现在,如果客户端在Accept报头指定“text/csv”,则服务器将返回CSV格式的数据。

The following example uses HttpClient to get the CSV data and write it to a file:
以下示例使用HttpClient来获取CSV数据,并将其写入一个文件:

HttpClient client = new HttpClient();
// Add the Accept header
// 添加Accept报头
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv"));
// Get the result and write it to a file.
// (Port 9000 is just an example port number.)
// 获取结果并将其写入文件
// (端口号9000只是一个示例端口号)
string result = client.GetStringAsync("http://localhost:9000/api/product/").Result;
System.IO.File.WriteAllText("products.csv", result);

【ASP.NET Web API教程】6.1 媒体格式化器的更多相关文章

  1. 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)

    原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  2. 【ASP.NET Web API教程】2.4 创建Web API的帮助页面

    原文:[ASP.NET Web API教程]2.4 创建Web API的帮助页面 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 ...

  3. 【ASP.NET Web API教程】2.3.2 创建域模型

    原文:[ASP.NET Web API教程]2.3.2 创建域模型 Part 2: Creating the Domain Models 第2部分:创建域模型 本文引自:http://www.asp. ...

  4. 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME

    原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...

  5. 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

    原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  6. 【ASP.NET Web API教程】6 格式化与模型绑定

    原文:[ASP.NET Web API教程]6 格式化与模型绑定 6 Formats and Model Binding 6 格式化与模型绑定 本文引自:http://www.asp.net/web- ...

  7. 【ASP.NET Web API教程】3 Web API客户端

    原文:[ASP.NET Web API教程]3 Web API客户端 Chapter 3: Web API Clients 第3章 Web API客户端 本文引自:http://www.asp.net ...

  8. 【ASP.NET Web API教程】2.3.7 创建首页

    原文:[ASP.NET Web API教程]2.3.7 创建首页 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 7: Crea ...

  9. 【ASP.NET Web API教程】2.3.6 创建产品和订单控制器

    原文:[ASP.NET Web API教程]2.3.6 创建产品和订单控制器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 6 ...

  10. 【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI

    原文:[ASP.NET Web API教程]2.3.5 用Knockout.js创建动态UI 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容 ...

随机推荐

  1. ubuntu 查看软件包版本以及软件包的源码

    aptitude show  xxx sudo apt-cache show autoconf setattr, getattr, setattr http://ju.outofmemory.cn/e ...

  2. [转]hadoop hdfs常用命令

    FROM : http://www.2cto.com/database/201303/198460.html hadoop hdfs常用命令   hadoop常用命令:  hadoop fs  查看H ...

  3. Linux Linux程序练习五

    题目:编写两个进程a和b,利用共享内存技术,a向共享内存写字符串,b将从共享内存中读到的字符串在屏幕上打印出来. //创建共享内存区 #include <stdio.h> #include ...

  4. MySQL基础 - 如何系统地学习数据库?

    对于数据库的认知,除了大学的时候上过数据库这门课,留下的印象大概就是几条SQL语句一些模棱两可的基本概念,直到工作后面临使用场景才发现数据库的重要性.故归纳总结一下自己的数据库学习之路. 学习资源: ...

  5. ThinkPHP项目CMS内容管理系统开发视频教程【20课】(3.02GB)

    ThinkPHP背景介绍:     ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业级应用开发而诞生的. ...

  6. 实验一(不知道怎么上传.docx格式)

    北京电子科技学院(BESTI) 实     验    报     告 课程:深入理解计算机系统        班级:1353 姓名:魏静静 文艺 刘虹辰 学号:20135302 20135331 20 ...

  7. IOS开发之—— iOS 支付 [支付宝、银联、微信]

    支付宝iOSsdk官方下载sdk地址:https://b.alipay.com/order/productDetail.htm?productId=2013080604609654&tabId ...

  8. StretchDIBits函数

    该函数将DIB中矩形区域内像素使用的颜色数据拷贝到指定的目标矩形中.如果目标矩形比源矩形大小要大,那么函数对颜色数据的行和列进行拉伸,以与目标矩形匹配.如果目标矩形大小要比源矩形小,那么该函数通过使用 ...

  9. WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点

    public class UIHelper { /// <summary> /// 在Visual里找到想要的元素 /// childName可为空,不为空就按名字找 /// </s ...

  10. 尝试EJB整合Mybatis部署时报错:获得带有类加载器MybatisUtil的ModuleClassLoader的反射信息出错,请问大神如何解决

    mybatis的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...