/// <summary>
        /// 生成模型
        /// </summary>
        /// <param name="request">请求数据</param>
        /// <param name="parameter">参数</param>       
        /// <returns></returns>
        public object BindModel(HttpRequest request, ParameterInfo parameter)
        {
            var name = parameter.Name;
            var targetType = parameter.ParameterType;

            if (targetType.IsClass == true && targetType.IsArray == false && targetType != typeof(string))
            {
                return ConvertToClass(request, parameter);
            }

            // 转换为数组
            var values = request.GetValues(name);
            if (targetType.IsArray == true)
            {
                return Converter.Cast(values, targetType);
            }

            // 转换为简单类型 保留参数默认值
            var value = values.FirstOrDefault();
            if (value == null && parameter.DefaultValue != DBNull.Value)
            {
                return parameter.DefaultValue;
            }
            return Converter.Cast(value, targetType);
        }
Exemple #2
0
 /// <summary>
 /// 收到http请求
 /// </summary>
 /// <param name="request"></param>
 /// <param name="response"></param>
 protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
 {
     var parameters = string.Join("&", request.Form, request.Query).Trim('&');
     if (string.Equals(request.Path, AppConfig.HttpURL.AbsolutePath, StringComparison.OrdinalIgnoreCase))
     {
         this.TryPushParameters(parameters);
     }
     response.Write(parameters);
 }
 /// <summary>
 /// 推送请求内容到客户端
 /// </summary>
 /// <param name="session">会话</param>
 /// <param name="request">请求</param>
 /// <returns></returns>
 private bool PushRequest(FastSession session, HttpRequest request)
 {
     try
     {
         session.InvokeApi("OnHttpRequest", request.Url.PathAndQuery, request.Body);
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
        /// <summary>
        /// 获取http行为
        /// 如果获取不到则返回null
        /// </summary>
        /// <param name="request">请求上下文</param>
        /// <returns></returns>
        public HttpAction TryGet(HttpRequest request)
        {
            var route = request.Url.AbsolutePath.ToLower();
            var apiAction = default(HttpAction);

            if (request.HttpMethod == HttpMethod.POST)
            {
                var postKey = route.GetHashCode() ^ HttpMethod.POST.GetHashCode();
                if (this.dictionary.TryGetValue(postKey, out apiAction))
                {
                    return apiAction;
                }
            }

            var allKey = route.GetHashCode() ^ HttpMethod_ALL.GetHashCode();
            this.dictionary.TryGetValue(allKey, out apiAction);
            return apiAction;
        }
        /// <summary>
        /// 生成模型
        /// </summary>
        /// <param name="request">请求数据</param>
        /// <param name="parameter">参数</param>       
        /// <returns></returns>
        public object BindModel(HttpRequest request, ParameterInfo parameter)
        {
            var name = parameter.Name;
            var type = parameter.ParameterType;

            if (type.IsClass == true && type.IsArray == false && type != typeof(string))
            {
                return ConvertToClass(request, parameter);
            }

            var values = request.GetValues(name);
            if (type.IsArray == true)
            {
                return ConvertToArray(type, name, values);
            }

            return ConvertToSimple(type, name, values.FirstOrDefault(), parameter.DefaultValue);
        }
        /// <summary>
        /// 转换为类
        /// </summary>
        /// <param name="parameter">参数</param>
        /// <param name="request">请求数据</param>
        /// <returns></returns>
        public static object ConvertToClass(HttpRequest request, ParameterInfo parameter)
        {
            var targetType = parameter.ParameterType;
            if (targetType.IsByRef && targetType.IsInterface && targetType.IsAbstract)
            {
                throw new NotSupportedException("不支持的类型:" + targetType.Name);
            }

            var instance = Activator.CreateInstance(targetType);
            var setters = PropertySetter.GetPropertySetters(targetType);
            foreach (var setter in setters)
            {
                var value = request.GetValues(setter.Name).FirstOrDefault();
                if (value != null)
                {
                    var valueCast = Converter.Cast(value, setter.Type);
                    setter.SetValue(instance, valueCast);
                }
            }
            return instance;
        }
        /// <summary>
        /// 收到Http请求
        /// </summary>       
        /// <param name="request">请求对象</param>
        /// <param name="response">回复对象</param>
        protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
        {
            var route = request.Url.AbsolutePath;
            var action = this.httpActionList.TryGet(route);
            var requestContext = new RequestContext(request, response);

            if (action == null)
            {
                var exception = new ApiNotExistException(route);
                var exceptionContext = new ExceptionContext(requestContext, exception);
                foreach (var filter in this.GlobalFilter.ExceptionFilters)
                {
                    filter.OnException(exceptionContext);
                    if (exceptionContext.ExceptionHandled == true) break;
                }

                var result = exceptionContext.Result == null ? new ErrorResult { Status = 404, Errors = exception.Message } : exceptionContext.Result;
                result.ExecuteResult(requestContext);
            }
            else
            {
                var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
                var actionContext = new ActionContext(requestContext, action);

                controller.Server = this;
                ((IHttpController)controller).Execute(actionContext);
            }
        }
 /// <summary>
 /// 请求上下文
 /// </summary>
 /// <param name="request">请求上下文对象</param>
 /// <param name="response">回复上下文对象</param>        
 internal RequestContext(HttpRequest request, HttpResponse response)
 {
     this.Request = request;
     this.Response = response;
 }
Exemple #9
0
 /// <summary>
 /// 收到Http请求
 /// </summary>       
 /// <param name="request">请求对象</param>
 /// <param name="response">回复对象</param>
 protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
 {
     Task.Factory.StartNew(() => this.ProcessRequest(new RequestContext(request, response)));
 }