/// <summary> /// 响应请求 /// </summary> public void Response() { ActionResult actionResult = null; try { //Initialize controller instance and get action information ActionInfo actionInfo = null; ControllerBase controllerInstance = InitializeControllerInstance(out actionInfo, context.Request.RawUrl.Split('?')[0]); if (controllerInstance == null || actionInfo == null) Log.Comment(CommentType.Error, "Controller或者对应Action未找到,可能请求的格式不正确(正确格式:{0}/ControllerName/ActionName?QueryString)。", SapiService.RootPath != null ? "/" + SapiService.RootPath : null); else { if (actionInfo.NeedAuthorize && (session == null || !session.IsAuthorized)) { controllerInstance = InitializeControllerInstance(out actionInfo, "/" + SapiService.RootPath + SapiService.NotAuthorized); if (controllerInstance != null && actionInfo != null) actionResult = (ActionResult)actionInfo.Action.Invoke(controllerInstance, null); else actionResult = new ActionNotAuthorized(); } else { controllerInstance.Session = this.session; controllerInstance.Parameters = GetRequestParameters(); controllerInstance.ServerConfigs = SapiService.ServerConfigs; actionResult = controllerInstance.PreResponse(); actionResult = actionResult ?? (ActionResult)actionInfo.Action.Invoke(controllerInstance, null); controllerInstance.PostResponse(); } } //Response if (actionResult == null) { context.Response.StatusCode = 404; } else { //添加请求的头部 if (actionResult.Headers != null) { foreach (string head in actionResult.Headers) { context.Response.Headers.Add(head); } } //下载文件请求 if (actionResult.Stream != null) { int receivedLength = 0; byte[] buffer = new byte[10240]; do { receivedLength = actionResult.Stream.Read(buffer, 0, buffer.Length); context.Response.OutputStream.Write(buffer, 0, receivedLength); } while (receivedLength > 0); actionResult.Stream.Close(); context.Response.StatusCode = 200; } else //数据请求 { byte[] buffer = Encoding.UTF8.GetBytes(actionResult.GetResultString()); context.Response.ContentLength64 = buffer.Length; context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.StatusCode = 200; } } } catch (Exception ex) { //关闭文件流 if (actionResult != null && actionResult.Stream != null) actionResult.Stream.Close(); context.Response.StatusCode = 500; Log.Comment(CommentType.Error, "未知错误:" + ex.Message); } finally { context.Response.Close(); } }
/// <summary> /// 响应请求 /// </summary> public void Response() { ActionResult actionResult = null; try { //Initialize controller instance and get action information ActionInfo actionInfo = null; ControllerBase controllerInstance = InitializeControllerInstance(out actionInfo, context.Request.RawUrl.Split('?')[0]); if (controllerInstance == null || actionInfo == null) { Log.Comment(CommentType.Error, "Controller或者对应Action未找到,可能请求的格式不正确(正确格式:{0}/ControllerName/ActionName?QueryString)。", SapiService.RootPath != null ? "/" + SapiService.RootPath : null); } else { if (actionInfo.NeedAuthorize && (session == null || !session.IsAuthorized)) { controllerInstance = InitializeControllerInstance(out actionInfo, "/" + SapiService.RootPath + SapiService.NotAuthorized); if (controllerInstance != null && actionInfo != null) { actionResult = (ActionResult)actionInfo.Action.Invoke(controllerInstance, null); } else { actionResult = new ActionNotAuthorized(); } } else { controllerInstance.Session = this.session; controllerInstance.Parameters = GetRequestParameters(); controllerInstance.ServerConfigs = SapiService.ServerConfigs; actionResult = controllerInstance.PreResponse(); actionResult = actionResult ?? (ActionResult)actionInfo.Action.Invoke(controllerInstance, null); controllerInstance.PostResponse(); } } //Response if (actionResult == null) { context.Response.StatusCode = 404; } else { //添加请求的头部 if (actionResult.Headers != null) { foreach (string head in actionResult.Headers) { context.Response.Headers.Add(head); } } //下载文件请求 if (actionResult.Stream != null) { int receivedLength = 0; byte[] buffer = new byte[10240]; do { receivedLength = actionResult.Stream.Read(buffer, 0, buffer.Length); context.Response.OutputStream.Write(buffer, 0, receivedLength); }while (receivedLength > 0); actionResult.Stream.Close(); context.Response.StatusCode = 200; } else //数据请求 { byte[] buffer = Encoding.UTF8.GetBytes(actionResult.GetResultString()); context.Response.ContentLength64 = buffer.Length; context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.StatusCode = 200; } } } catch (Exception ex) { //关闭文件流 if (actionResult != null && actionResult.Stream != null) { actionResult.Stream.Close(); } context.Response.StatusCode = 500; Log.Comment(CommentType.Error, "未知错误:" + ex.Message); } finally { context.Response.Close(); } }