Asp.Net Web API 2第十二课——Media Formatters媒体格式化器
前言
阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html
本教程演示如何在ASP.NET Web API中支持额外的媒体格式。
Internet Media Types——Internet的媒体类型
媒体类型,也叫做MIME类型,标识了一片数据的格式。在HTTP中,媒体类型描述了消息体的格式。一个媒体类型由两个字符串组成:类型和子类型。例如:
- text/html
- image/png
- application/json
当一条HTTP消息含有一个实体时,Content-Type(内容类型)报头指定消息体的格式。这是告诉接收器如何解析消息体的内容。
例如,如果一个HTTP响应含有一个PNG图片,该响应可能会有以下报头。
HTTP/1.1 OK
Content-Length:
Content-Type: image/png
当客户端发送一条请求消息时,它可能包括一个Accept报头。Accept报头是告诉服务器,客户端希望从服务器得到哪种媒体类型。例如:
Accept: text/html,application/xhtml+xml,application/xml
该报头告诉服务器,客户端希望得到的是HTML、XHTML,或XML。
在Web API中,媒体类型决定了Web API如何对HTTP消息体进行序列化和反序列化。对于XML、JSON,以及URL编码的表单数据,已有了内建的支持。而且,通过编写媒体格式化器(Media Formatter),可以支持额外的媒体类型。
为了创建媒体格式化器,需从以下类进行派生:
- MediaTypeFormatter。这个类使用了异步读写方法
- BufferedMediaTypeFormatter。这个类派生于MediaTypeFormatter,但将异步读写方法封装在同步方法之中。
从BufferedMediaTypeFormatter派生要更简单些,因为没有异步代码,但它也意味着在I/O期间可能会阻塞线程。
Creating a Media Formatter——创建媒体格式化器
以下示例演示了一个媒体类型格式化器,它可以将Product对象序列化成一个逗号分隔的值(CSV)格式。该示例使用了Asp.Net Web API 2第二课——CRUD操作 http://www.cnblogs.com/aehyok/p/3434578.html中定义的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; }
}
}
为了实现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
{
}
}
在其构造器中,要添加一个该格式化器所支持的媒体类型。在这个例子中,该格式化器只支持单一的媒体类型:“text/csv”:
public ProductCsvFormatter()
{
// Add the supported media type.
// 添加所支持的媒体类型
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/csv"));
}
重写这个CanWriteType方法,以指示该格式化器可以序列化哪种类型:
public override bool CanWriteType(System.Type type)
{
if (type == typeof(Product))
{
return true;
}
else
{
Type enumerableType = typeof(IEnumerable<Product>);
return enumerableType.IsAssignableFrom(type);
}
}
在这个例子中,格式化器可以序列化单个Product对象,以及Product对象集合。
相应地,重写CanReadType方法,以指示该格式化器可以反序列化哪种类型。在此例中,格式化器不支持反序列化,因此该方法简单地返回false。
protected override bool CanReadType(Type type)
{
return false;
}
最后,重写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) != -)
{
return String.Format("\"{0}\"", field.Replace("\"", "\"\""));
}
else return field;
}
Adding the Media Formatter——添加媒体格式化器
为了将媒体类型格式化器添加到Web API管线,要使用HttpConfiguration对象上的Formatters属性。
public static void ConfigureApis(HttpConfiguration config)
{
config.Formatters.Add(new ProductCsvFormatter());
}
对于ASP.NET托管,要将这个函数添加到Global.asax文件,并通过Application_Start方法调用它。
protected void Application_Start()
{
ConfigureApis(GlobalConfiguration.Configuration); // ...
}
现在,如果客户端在Accept报头指定“text/csv”,则服务器将返回CSV格式的数据。
以下示例使用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 2第十二课——Media Formatters媒体格式化器的更多相关文章
- Asp.Net Web API 2第十八课——Working with Entity Relations in OData
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文的示例代码的下载地址 ...
- Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)
导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文主要来讲解以下内容: ...
- Asp.Net Web API 2第十五课——Model Validation(模型验证)
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文参考链接文章地址htt ...
- Asp.Net Web API 2第十四课——Content Negotiation(内容协商)
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
- ASP.NET Web API 控制器创建过程(二)
ASP.NET Web API 控制器创建过程(二) 前言 本来这篇随笔应该是在上周就该写出来发布的,由于身体跟不上节奏感冒发烧有心无力,这种天气感冒发烧生不如死,也真正的体会到了什么叫病来如山倒,病 ...
- Kali Linux Web 渗透测试— 第十二课-websploit
Kali Linux Web 渗透测试— 第十二课-websploit 文/玄魂 目录 Kali Linux Web 渗透测试— 第十二课-websploit..................... ...
- Asp.Net Web API 2(CRUD操作)第二课
Asp.Net Web API 2(CRUD操作)第二课 Asp.Net Web API 导航 Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok ...
- 使用ASP.NET web API创建REST服务(二)
Creating a REST service using ASP.NET Web API A service that is created based upon the architecture ...
- 细说Asp.Net Web API消息处理管道(二)
在细说Asp.Net Web API消息处理管道这篇文章中,通过翻看源码和实例验证的方式,我们知道了Asp.Net Web API消息处理管道的组成类型以及Asp.Net Web API是如何创建消息 ...
随机推荐
- struts2 xml中重定向
<result name="success" type="redirect">ManagePartAction</result> 重定向 ...
- 关于64位windows2003 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 的问题
我了个去啊! 在自己机器上测试通过的excel导入功能在客户服务器上死活都不好用,查了半天后来发现客户服务器是64位的win2003!! try catch捕捉问题为:未在本地计算机上注册“Micro ...
- Unix网络编程 -- ubuntu下搭建编译环境( 解决unp.h 编译等问题)
1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下载unpv13e http://ishare.i ...
- mysqldb模块的简单用法
# - *- coding:utf-8-*-import urllib2import reimport MySQLdbimport sysreload(sys)sys.setdefaultencodi ...
- Spring 定时执行任务
好不容易写了半天的文章竟然由于断网而丢失了,并未自动保存到草稿箱.只能简单的贴贴代码了. <?xml version="1.0" encoding="UTF-8&q ...
- java文件末尾追加内容的两种方式
java 开发中,偶尔会遇到在文件末尾对文件内容进行追加,实际上有多种方式可以实现,简单介绍两种: 一种是通过RandomAccessFile类实现,另一种是通过FileWriter类来实现. 实现方 ...
- SQL Server Profiler使用方法
一.SQL Server Profiler使用方法 1.单击开始--程序--Microsoft SQL Server 2005--性能工具--SQL Server Profiler,如下图: 2. ...
- vim--golang代码补全
我想说,我折腾了很久编辑器,试了九种办法 最后我只成功了一种 但我依然想就我混乱的逻辑做下整理 一.一开始,我试图入手ipad编码软件,大概9美金吧,叫Textastic.我试图用它的近亲来试验Tex ...
- jquery 滚动到底部加载
var body_ = $(window); var indexPage = 2; var pageCount = <?php echo $pageCount;?>; var _ajaxR ...
- 技术英文单词贴--S
S separator 分离,隔离器 shortcut 快捷方式,捷径 sort 排序 special 特殊的,专用的 specified 规定的,详细说明,指定 specify 指定,明确提出,详细 ...