zoukankan      html  css  js  c++  java
  • 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[] { ',', '
    ', '
    ', '"' };
            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

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

             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);
            }
  • 相关阅读:
    gitlab Failed to register this runner. Perhaps you are having network problems runner 注册失败问题解决
    gitlab pipelines 使用
    centos 7 bbr 安装
    kafka ksql && docker 安装试用
    netflix vector 系统性能监控安装使用
    keycloak && docker安装 &&spring boot 集成使用
    gogs docker 安装
    alpine docker 镜像 时区问题
    nginx 流量拷贝模块 ngx_http_mirror_module 安装试用
    ulimit  设置
  • 原文地址:https://www.cnblogs.com/goodlucklzq/p/4447912.html
Copyright © 2011-2022 走看看