public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (candidates == null) { throw new ArgumentNullException(nameof(candidates)); } for (var i = 0; i < candidates.Count; i++) { ref var candidate = ref candidates[i]; var endpoint = (RouteEndpoint)candidate.Endpoint; var page = endpoint.Metadata.GetMetadata <PageActionDescriptor>(); if (page != null) { var compiled = _loader.Load(page); candidates.ReplaceEndpoint(i, compiled.Endpoint, candidate.Values); } }
public IEnumerable <RouteInformation> GetAllRouteInformations() { List <RouteInformation> list = new List <RouteInformation>(); var actionDescriptors = this._actionDescriptorCollectionProvider.ActionDescriptors.Items; foreach (var actionDescriptor in actionDescriptors) { RouteInformation info = new RouteInformation(); if (actionDescriptor.RouteValues.ContainsKey("area")) { info.AreaName = actionDescriptor.RouteValues["area"]; } if (actionDescriptor is PageActionDescriptor pageActionDescriptor) { var compiledPage = _pageLoader.Load(pageActionDescriptor); info.Path = pageActionDescriptor.ViewEnginePath; info.Invocation = pageActionDescriptor.RelativePath; SetHtmlInfo(info, compiledPage); if (!list.Exists(x => x.Invocation == info.Invocation)) { list.Add(info); } continue; } if (actionDescriptor.AttributeRouteInfo != null) { info.Path = $"/{actionDescriptor.AttributeRouteInfo.Template}"; } // Controller/Action if (actionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { if (info.Path.IsEmpty()) { info.Path = $"/{controllerActionDescriptor.ControllerName}/{controllerActionDescriptor.ActionName}"; } SetHtmlInfo(info, controllerActionDescriptor); info.ControllerName = controllerActionDescriptor.ControllerName; info.ActionName = controllerActionDescriptor.ActionName; info.Invocation = $"{controllerActionDescriptor.ControllerName}Controller.{controllerActionDescriptor.ActionName}"; } info.Invocation += $"({actionDescriptor.DisplayName})"; list.Add(info); } return(list); }
public async Task Invoke(HttpContext context) { var relativePath = context.Request.Path.Value.TrimStart(_separator); if (relativePath == string.Empty) { relativePath = _defaultPage; } if (relativePath.Contains(".")) { if (relativePath.EndsWith(Page.Extension)) { var extensionIndex = relativePath.LastIndexOf(Page.Extension); relativePath = relativePath.Remove(extensionIndex); } else { await _next(context); } } var pageContext = _pageContextFactory.Create(context); var pageType = _pageLoader.Load(relativePath); if (pageType == null) { context.Response.StatusCode = 404; return; } pageContext.PageDescriptor = new PageDescriptor { RelativePath = relativePath, PageType = pageType }; var page = _pageFactory.CreatePage(pageContext); await page.ExecuteAsync(); }
/// <summary> /// 构建并生成Html /// </summary> public async Task BuildAsync() { var pageActionDescriptors = GetPageActionDescriptors(); var requestUrl = $"{Web.Request.Scheme}://{Web.Request.Host}"; foreach (var actionDescriptor in pageActionDescriptors) { if (actionDescriptor.RelativePath.ToLower() == "/Pages/Index.cshtml".ToLower()) { continue; } var compiledPage = _pageLoader.Load(actionDescriptor); var path = actionDescriptor.ViewEnginePath;// Url访问路径:/Components/DataDisplay/Table var htmlPath = GetHtmlPathAttribute(compiledPage); if (htmlPath != null) { if (htmlPath.Ignore) { continue; } } await Web.Client <string>().Get($"{requestUrl}/view{path}").ResultAsync(); } }
public void OnProvidersExecuting(ActionInvokerProviderContext context) { var actionDescriptor = context.ActionContext.ActionDescriptor as PageActionDescriptor; if (actionDescriptor != null) { var itemCount = actionDescriptor.FilterDescriptors?.Count ?? 0; var filterItems = new List <FilterItem>(itemCount); for (var i = 0; i < itemCount; i++) { var item = new FilterItem(actionDescriptor.FilterDescriptors[i]); filterItems.Add(item); } var filterProviderContext = new FilterProviderContext(context.ActionContext, filterItems); for (var i = 0; i < _filterProviders.Length; i++) { _filterProviders[i].OnProvidersExecuting(filterProviderContext); } for (var i = _filterProviders.Length - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(filterProviderContext); } var filters = new IFilterMetadata[filterProviderContext.Results.Count]; for (var i = 0; i < filterProviderContext.Results.Count; i++) { filters[i] = filterProviderContext.Results[i].Filter; } var compiledType = _loader.Load(actionDescriptor); var compiledActionDescriptor = new CompiledPageActionDescriptor(actionDescriptor) { PageType = compiledType.GetTypeInfo(), HandlerMethods = new List <HandlerMethodDescriptor>(), }; foreach (var method in compiledType.GetTypeInfo().GetMethods()) { if (method.Name.StartsWith("OnGet") || method.Name.StartsWith("OnPost")) { compiledActionDescriptor.HandlerMethods.Add(new HandlerMethodDescriptor() { Method = method, }); } } context.Result = new PageActionInvoker( _diagnosticSource, _logger, _factory, _selector, _metadataProvider, _tempDataFactory, _viewOptions, filters, _valueProviderFactories, context.ActionContext, compiledActionDescriptor, _pagePersistedPropertyProvider); } }
private IEnumerable <AreaInfo> GetAreaInfos(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider, IPageLoader pageLoader, LinkGenerator linkGenerator) { //根,从area开始 var result = new List <AreaInfo>(); //遍历区域 foreach (var area in actionDescriptorCollectionProvider.ActionDescriptors.Items.GroupBy(item => item.RouteValues["area"])) { var areaInfo = new AreaInfo { Name = area.Key }; result.Add(areaInfo); //遍历控制器 foreach (var controllerActions in area.OfType <ControllerActionDescriptor>().GroupBy(c => c.ControllerTypeInfo)) { //跳过重复控制器,不知道为什么,里面有些重复项目 if (areaInfo.Controllers.Any(c => c.TypeFullName == controllerActions.Key.FullName)) { continue; } var conInfo = new AreaInfo.ControllerInfo { Name = controllerActions.First().ControllerName, TypeFullName = controllerActions.Key.FullName, Description = (controllerActions.Key.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) ?.Description, Area = controllerActions.First().RouteValues["area"], }; areaInfo.Controllers.Add(conInfo); //遍历动作 foreach (var action in controllerActions) { var act = new AreaInfo.ControllerInfo.ActionInfo { Name = action.MethodInfo.Name, ActionName = action.ActionName, HandlerIdentification = (action.MethodInfo.GetCustomAttribute(typeof(RequestHandlerIdentificationAttribute)) as RequestHandlerIdentificationAttribute) ?.UniqueKey, Signature = action.MethodInfo.ToString(), Description = (action.MethodInfo.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) ?.Description, Url = linkGenerator.GetPathByAction(action.ActionName, conInfo.Name, new { area = conInfo.Area }), HttpMethodLimits = action.ActionConstraints?.OfType <HttpMethodActionConstraint>().FirstOrDefault()?.HttpMethods.ToList(), MethodInfo = action.MethodInfo, ActionDescriptor = action }; conInfo.Actions.Add(act); } } //遍历Razor Page foreach (var pageActionDescriptor in area.OfType <PageActionDescriptor>()) { //载入Razor Page,会从程序集加载或编译page文件并缓存 var page = pageLoader.Load(pageActionDescriptor); //也是剔除重复 if (areaInfo.Pages.Any(p => p.TypeFullName == page.HandlerTypeInfo.FullName)) { continue; } var pageInfo = new AreaInfo.PageInfo { Name = page.DisplayName, Area = page.AreaName, TypeFullName = page.HandlerTypeInfo.FullName, Description = (page.HandlerTypeInfo.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) ?.Description }; foreach (var handlerMethod in page.HandlerMethods) { pageInfo.PageHandlers.Add(new AreaInfo.PageInfo.PageHandlerInfo { HttpMethod = handlerMethod.HttpMethod, Name = handlerMethod.Name, HandlerIdentification = (handlerMethod.MethodInfo.GetCustomAttribute(typeof(RequestHandlerIdentificationAttribute)) as RequestHandlerIdentificationAttribute) ?.UniqueKey, Signature = handlerMethod.MethodInfo.ToString(), Description = (handlerMethod.MethodInfo.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) ?.Description, Url = linkGenerator.GetPathByPage(page.DisplayName, handlerMethod.Name, new { area = page.AreaName }), MethodInfo = handlerMethod.MethodInfo, ActionDescriptor = page }); } areaInfo.Pages.Add(pageInfo); } } return(result); }
public void OnProvidersExecuting(ActionInvokerProviderContext context) { var actionDescriptor = context.ActionContext.ActionDescriptor as PageActionDescriptor; if (actionDescriptor != null) { var itemCount = actionDescriptor.FilterDescriptors?.Count ?? 0; var filterItems = new List <FilterItem>(itemCount); for (var i = 0; i < itemCount; i++) { var item = new FilterItem(actionDescriptor.FilterDescriptors[i]); filterItems.Add(item); } var filterProviderContext = new FilterProviderContext(context.ActionContext, filterItems); for (var i = 0; i < _filterProviders.Length; i++) { _filterProviders[i].OnProvidersExecuting(filterProviderContext); } for (var i = _filterProviders.Length - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(filterProviderContext); } var filters = new IFilterMetadata[filterProviderContext.Results.Count]; for (var i = 0; i < filterProviderContext.Results.Count; i++) { filters[i] = filterProviderContext.Results[i].Filter; } var compiledType = _loader.Load(actionDescriptor).GetTypeInfo(); var modelType = compiledType.GetProperty("Model")?.PropertyType.GetTypeInfo(); var compiledActionDescriptor = new CompiledPageActionDescriptor(actionDescriptor) { ModelType = modelType, PageType = compiledType, HandlerMethods = new List <HandlerMethodDescriptor>(), }; if (modelType != null && modelType != compiledType) { // If the model and page type are different discover handler methods on the model as well. PopulateHandlerMethodDescriptors(modelType, compiledActionDescriptor); } PopulateHandlerMethodDescriptors(compiledType, compiledActionDescriptor); context.Result = new PageActionInvoker( _diagnosticSource, _logger, _factory, _modelFactory, _selector, _metadataProvider, _tempDataFactory, _viewOptions, filters, _valueProviderFactories, context.ActionContext, compiledActionDescriptor, _pagePersistedPropertyProvider); } }