/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route"></param> /// <param name="request"></param> /// <returns></returns> protected IResponse RunRouteWithMiddleware(Route route, Request request) { var response = new Response(); try { container.Instance(typeof(IResponse).ToString(), response); responseStack.Push(response); var middleware = route.GatherMiddleware(); if (middleware != null) { middleware.Do(request, response, (req, res) => { PrepareResponse(req, route.Run(req as Request, res as Response)); }); } else { PrepareResponse(request, route.Run(request, response)); } return(response); }catch (Exception ex) { var chain = route.GatherOnError(); if (chain != null) { chain.Do(request, response, ex, (req, res, error) => { ThrowOnError(request, response, ex); }); } else { ThrowOnError(request, response, ex); } return(null); } finally { responseStack.Pop(); if (responseStack.Count > 0) { container.Instance(typeof(IResponse).ToString(), responseStack.Peek()); } else { container.Instance(typeof(IResponse).ToString(), null); } } }
/// <summary> /// 执行路由请求 /// </summary> /// <param name="route">路由</param> /// <param name="request">请求</param> /// <param name="response">响应</param> /// <returns>响应</returns> private IResponse RunInRoute(Route route, Request request, Response response) { var middleware = route.GatherMiddleware(); if (middleware != null) { middleware.Do(request, response, (req, res) => { PrepareResponse(req, route.Run(req as Request, res as Response)); }); } else { PrepareResponse(request, route.Run(request, response)); } return(response); }
/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route"></param> /// <param name="request"></param> /// <returns></returns> protected IResponse RunRouteWithMiddleware(Route route, Request request) { try { var response = new Response(); container.Instance(typeof(IResponse).ToString(), response); var middleware = route.GatherMiddleware(); if (middleware != null) { middleware.Then((req, res) => { PrepareResponse(req, route.Run(req as Request, res as Response)); }).Do(request, response); } else { PrepareResponse(request, route.Run(request, response)); } return(response); }catch (Exception ex) { var chain = route.GatherOnError(); if (chain != null) { chain.Then((req, error) => { ThrowOnError(request, ex); }).Do(request, ex); } else { ThrowOnError(request, ex); } return(null); } }