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);
        }
    }
  • 相关阅读:
    [转]快速矩阵快速幂
    继续学习C:数字进制表示
    pthread_cond_wait()用法分析
    [原]NYOJ-光棍的yy-655
    [原]NYOJ-组合数-32
    [转]_int64、long long 的区别
    [原]NYOJ-6174问题-57
    [转]sscanf函数具体用法
    [原]NYOJ-A*B Problem II-623
    集存款(复利单利)贷款为一体的计算器(最新版)
  • 原文地址:https://www.cnblogs.com/kingCpp/p/4764117.html
Copyright © 2011-2022 走看看