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是如何创建消息 ...
随机推荐
- 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
原因是EntityUtils.toString(HttpEntity)方法被使用了多次.所以每个方法内只能使用一次.
- linux 下tomcat catalina.out日志操作
1. 查看日志 tail -f catalina.out 会动态打印日志. 2. 查看所有日志 less -f catalina.out 打开所有日志后,默认是显示第一页,常用命令用到如下: G:到达 ...
- java 封装httpclient 的get 和post 请求
import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.util. ...
- Python_sklearn机器学习库学习笔记(一)_Feature Extraction and Preprocessing(特征提取与预处理)
# Extracting features from categorical variables #Extracting features from categorical variables 独热编 ...
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- Xcode 设置代码不自动换行
"command + ,"打开设置界面后,找到"Text Editing" 然后选择"Indentation", 最后找到"lin ...
- Java.web-application-development-environments-for-macosx
Java Web Application开发 1 下载需要的软件 使用的软件并没有采用最新的版本,只是采用了次新版本. 1.1 下载Eclipse的jee版本 eclipse-jee-luna-SR2 ...
- Python.resource-for-python-from-internet
1. pyvideo Python related video indexed so you can find it. http://pyvideo.org/ 2. 6 Useful Python L ...
- WinForm窗体PropertyGrid控件的使用
使用过 Microsoft Visual Basic 或 Microsoft Visual Studio .NET的朋友,一定使用过属性浏览器来浏览.查看或编辑一个或多个对象的属性..NET 框架 P ...
- 第一章 git指令与设置
相关指令: 1.从远程的master分支上创建新的分支,此时新分支内容与master分支内容相同: git checkout master; git branch newbranch; git che ...