zoukankan      html  css  js  c++  java
  • web api同源策略

    1.重写JsonMediaTypeFormatter

    public class JsonpMediaTypeFormatter : JsonMediaTypeFormatter
    {
      private string _callbackQueryParameter;
    
      public JsonpMediaTypeFormatter()
      {
        SupportedMediaTypes.Add(DefaultMediaType);
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/javascript"));
    
        // need a lambda here so that it'll always get the 'live' value of CallbackQueryParameter.
        MediaTypeMappings.Add(new Mapping(() => CallbackQueryParameter, "application/javascript"));
      }
    
      public string CallbackQueryParameter
      {
        get { return _callbackQueryParameter ?? "callback"; }
        set { _callbackQueryParameter = value; }
      }
    
      public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
                                              TransportContext transportContext)
      {
        var callback = GetCallbackName();
    
        if (!String.IsNullOrEmpty(callback))
        {
          // select the correct encoding to use.
          Encoding encoding = SelectCharacterEncoding(content.Headers);
    
          // write the callback and opening paren.
          return Task.Factory.StartNew(() =>
            {
              var bytes = encoding.GetBytes(callback + "(");
              writeStream.Write(bytes, 0, bytes.Length);
            })
          // then we do the actual JSON serialization...
          .ContinueWith(t => base.WriteToStreamAsync(type, value, writeStream, content, transportContext))
    
          // finally, we close the parens.
          .ContinueWith(t =>
            {
              var bytes = encoding.GetBytes(")");
              writeStream.Write(bytes, 0, bytes.Length);
            });
        }
        return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
      }
    
      private string GetCallbackName()
      {
        if (HttpContext.Current.Request.HttpMethod != "GET")
          return null;
        return HttpContext.Current.Request.QueryString[CallbackQueryParameter];
      }
    
      #region Nested type: Mapping
    
      private class Mapping : MediaTypeMapping
      {
        private readonly Func<string> _param; 
    
        public Mapping(Func<string> discriminator, string mediaType)
          : base(mediaType)
        {
          _param = discriminator;
        }
    
        public override double TryMatchMediaType(HttpRequestMessage request)
        {
          if (request.RequestUri.Query.Contains(_param() + "="))
            return 1.0;
          return 0.0;
        }
      }
    
      #endregion
    }

    在global中注册

      GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter ());
    2.actionfilter
    public class JsonCallbackAttribute
        : ActionFilterAttribute
    {
        private const string CallbackQueryParameter = "callback";
    
        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            var callback = context.Request.GetQueryNameValuePairs().Where(item => item.Key == CallbackQueryParameter).Select(item => item.Value).SingleOrDefault();
    
            if (!string.IsNullOrEmpty(callback))
            {
                var jsonBuilder = new StringBuilder(callback);
    
                jsonBuilder.AppendFormat("({0})", context.Response.Content.ReadAsStringAsync().Result);
    
                context.Response.Content = new StringContent(jsonBuilder.ToString());
            }
    
            base.OnActionExecuted(context);
        }
    }
  • 相关阅读:
    《贝叶斯优化: 一种更好的超参数调优方式》
    《从Google Visor到Microsoft NNI再到Advisor调参服务接口发展史》
    《归一化激活层的进化:谷歌Quoc Le等人利用AutoML 技术发现新型ML模块》
    《通用视觉 & NAS》
    《Object Detection-IOU Net笔记》
    《Detectron2之ROIAlign》
    《IoUNet(6)_源码_RoIAlign(1)》
    聊天机器人资源合集:项目,语聊,论文,教程。
    一个使用 Python 的人工智能聊天机器人框架
    【TensorFlow 官网 可以直接访问】让中国开发者更容易地使用TensorFlow打造人工智能应用
  • 原文地址:https://www.cnblogs.com/kingCpp/p/4764117.html
Copyright © 2011-2022 走看看