public HttpContextBase PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution) { Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context"); var urlParts = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray()).Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray(); var controllerName = urlParts[0]; var controllerAction = urlParts[1]; var requestContext = new RequestContext( XmlDbUpdateHttpContextHelpers.BuildHttpContextBase(recordedAction, backendUrl, userId, useGuidSubstitution), XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controllerName, controllerAction) ); BackendActionContext.ResetCurrent(); XmlDbUpdateHttpContextHelpers.BuildController(requestContext, controllerName, CultureHelpers.GetCultureByLcid(recordedAction.Lcid)).Execute(requestContext); return(requestContext.HttpContext); }
public HttpContext PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution, IServiceProvider provider = null) { Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context"); var urlParts = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray()) .Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray(); var controller = urlParts[0]; var action = urlParts[1]; BackendActionContext.ResetCurrent(); var cultureInfo = CultureHelpers.GetCultureByLcid(recordedAction.Lcid); CultureInfo.CurrentCulture = cultureInfo; var httpContext = XmlDbUpdateHttpContextHelpers.BuildHttpContext(recordedAction, backendUrl, userId, useGuidSubstitution); var routeData = XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controller, action); httpContext.Features[typeof(IRoutingFeature)] = new RoutingFeature { RouteData = routeData }; var routeContext = new RouteContext(httpContext) { RouteData = routeData }; var serviceProvider = provider ?? new HttpContextAccessor().HttpContext.RequestServices; httpContext.RequestServices = serviceProvider; var m = typeof(DynamicRouteValueTransformer).Assembly.GetType("Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandler"); var handler = (IRouter)serviceProvider.GetRequiredService(m); QPContext.CurrentUserId = userId; Task.Run(async() => { await handler.RouteAsync(routeContext); await routeContext.Handler(routeContext.HttpContext); }).Wait(); QPContext.CurrentUserId = 0; return(httpContext); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { try { var isValid = filterContext.Exception == null && filterContext.Controller.ViewData.ModelState.IsValid && !QPController.IsError(filterContext.HttpContext); if (isValid && DbRepository.Get().RecordActions) { var currentDbVersion = new ApplicationInfoRepository().GetCurrentDbVersion(); var actionToSerialize = XmlDbUpdateHttpContextHelpers.CreateXmlDbUpdateActionFromHttpContext(filterContext.HttpContext, _code ?? BackendActionContext.Current.ActionCode, _ignoreForm); XmlDbUpdateSerializerHelpers .SerializeAction(actionToSerialize, currentDbVersion, CommonHelpers.GetBackendUrl(filterContext.HttpContext)) .Save(QPContext.GetRecordXmlFilePath(), SaveOptions.None); } } catch (Exception ex) { throw new Exception("There was an error while recording xml actions", ex); } base.OnActionExecuted(filterContext); }