WebApi2官网学习记录---Media Formatters

Web API内建支持XML、JSON、BSON、和form-urlencoded的MiME type。

创建的自定义MIME类型要继承一下类中的一个:

自定义一个MIME类型,如下: 

  public class ProductCsvFormatter:BufferedMediaTypeFormatter

    {

        public ProductCsvFormatter()

        {

            //媒体类型

            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/csv"));

            //支持的字符集

            SupportedEncodings.Add(Encoding.GetEncoding("iso-8859-1"));

            SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier:false));

            

        }

        //指示哪种类型 格式化器可以进行序列化,

        public override bool CanWriteType(Type type)

        {//支持序列化翻个Product对象和Product对象集合

            if (type == typeof(Product))

                return true;

            else

            {

                Type enumerableType = typeof(IEnumerable<Product>);

                return enumerableType.IsAssignableFrom(type);

            }

        }

        //指示哪个对象可以被反序列化

        public override bool CanReadType(Type type)

        {//目前不需要反序列化 所以返回false

            return false;

        }

        //序列化对象并写入流,如果支持反序列化还需要重载ReadFromStream方法

        public override void WriteToStream(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content)

        {

            Encoding effectiveEncoding = SelectCharacterEncoding(content.Headers);

            using (var writer = new StreamWriter(writeStream, effectiveEncoding))

            {

                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);

                }

            }

        }

        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;



        }

    }



 
 public static void Register(HttpConfiguration config)

        {

            // Web API 配置和服务



            // Web API 路由

            config.MapHttpAttributeRoutes();



            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{id}",

                defaults: new { id = RouteParameter.Optional }

            );

            //添加自定义的MIME类型

            config.Formatters.Add(new ProductCsvFormatter());

        }

模拟请求如图:请求时要指定Content-Type

WebApi2官网学习记录---Media Formatters

WebApi2官网学习记录---Media Formatters

或者在控制台模拟一个请求:

         static void Main(string[] args)

        {

            HttpClient client = new HttpClient();            

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv"));  

            string result = client.GetStringAsync("http://localhost:53330/api/products/").Result;

            System.IO.File.WriteAllText("d:\\products.csv", result);

        }

 

你可能感兴趣的:(format)