HttpRequestMessage扩展方法

 

 

public static class HttpRequestMessageExtensions
    {
        /// 
        /// Gets the  for the given request.
        /// 
        /// The HTTP request.
        /// The .
        public static HttpConfiguration GetConfiguration(this HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

            return request.GetProperty(HttpPropertyKeys.HttpConfigurationKey);
        }

        /// 
        /// Gets the  for the given request or null if not available.
        /// 
        /// The HTTP request.
        /// The  or null.
        public static SynchronizationContext GetSynchronizationContext(this HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

            return request.GetProperty(HttpPropertyKeys.SynchronizationContextKey);
        }

        /// 
        /// Gets the  for the given request or null if not available.
        /// 
        /// The HTTP request.
        /// The  or null.
        public static IHttpRouteData GetRouteData(this HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

            return request.GetProperty(HttpPropertyKeys.HttpRouteDataKey);
        }

        public static T GetProperty(this HttpRequestMessage request, string key)
        {
            T value = default(T);
            object @object = null;
            request.Properties.TryGetValue(key, out @object);
            if (@object is T)
            {
                value = (T)@object;
            }
            else
            {
                throw new InvalidCastException(string.Format("无效类型转换:{0}", typeof(T).FullName));
            }
            return value;
        }
        
    }

你可能感兴趣的:(HttpRequestMessage扩展方法)