/// <summary> /// /// </summary> /// <param name="info"></param> /// <returns></returns> protected virtual string GetPath(RouteInformation info) { if (!info.IsPageRoute) { var area = info.AreaName.SafeString(); var controller = info.ControllerName.SafeString(); var action = info.ActionName.SafeString(); var path = info.TemplatePath.Replace("{area}", area).Replace("{controller}", controller).Replace("{action}", action); return(path.ToLower()); } if (!string.IsNullOrWhiteSpace(info.FilePath)) { return(info.FilePath.ToLower()); } var paths = info.Path.TrimStart('/').Split('/'); if (paths.Length >= 3) { return(info.TemplatePath.Replace("{area}", paths[0]) .Replace("{controller}", paths[1]) .Replace("{action}", JoinActionUrl(paths))); } return(string.Empty); }
/// <summary> /// /// </summary> /// <param name="info"></param> /// <returns></returns> public async Task WriteViewToFileAsync(RouteInformation info) { try { var html = info.IsPageRoute ? await RenderPageViewToStringAsync(info) : await RenderActionViewToStringAsync(info); if (string.IsNullOrWhiteSpace(html)) { return; } var path = Orion.Framework.Helpers.WebHttp.GetPhysicalPath(string.IsNullOrWhiteSpace(info.FilePath) ? GetPath(info) : info.FilePath); var directory = System.IO.Path.GetDirectoryName(path); if (string.IsNullOrWhiteSpace(directory)) { return; } if (Directory.Exists(directory) == false) { Directory.CreateDirectory(directory); } System.IO.File.WriteAllText(path, html); } catch (Exception ex) { ex.Log(Log.GetLog().Caption("HTML")); } }
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); }
private void SetHtmlInfo(RouteInformation routeInformation, ControllerActionDescriptor controllerActionDescriptor) { var htmlAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttribute <HtmlAttribute>() ?? controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute <HtmlAttribute>(); if (htmlAttribute == null) { return; } routeInformation.FilePath = htmlAttribute.Path; routeInformation.TemplatePath = htmlAttribute.Template; routeInformation.IsPartialView = htmlAttribute.IsPartialView; routeInformation.ViewName = htmlAttribute.ViewName; }
private void SetHtmlInfo(RouteInformation routeInformation, CompiledPageActionDescriptor compiledPageActionDescriptor) { routeInformation.IsPageRoute = true; var htmlAttribute = compiledPageActionDescriptor.PageTypeInfo.GetCustomAttribute <HtmlAttribute>() ?? compiledPageActionDescriptor.DeclaredModelTypeInfo.GetCustomAttribute <HtmlAttribute>(); if (htmlAttribute == null) { return; } routeInformation.FilePath = htmlAttribute.Path; routeInformation.TemplatePath = htmlAttribute.Template; routeInformation.IsPartialView = htmlAttribute.IsPartialView; routeInformation.ViewName = htmlAttribute.ViewName; }
/// <summary> /// /// </summary> /// <param name="info"></param> /// <returns></returns> private async Task <string> RenderActionViewToStringAsync(RouteInformation info) { var razorViewEngine = Ioc.Create <IRazorViewEngine>(); var tempDataProvider = Ioc.Create <ITempDataProvider>(); var serviceProvider = Ioc.Create <IServiceProvider>(); var httpContext = new DefaultHttpContext { RequestServices = serviceProvider }; var actionContext = new ActionContext(httpContext, GetRouteData(info), new ActionDescriptor()); var viewResult = GetView(razorViewEngine, actionContext, info); if (!viewResult.Success) { throw new InvalidOperationException($" {info.ActionName}"); } using (var stringWriter = new StringWriter()) { var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); var viewContext = new ViewContext(actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, tempDataProvider), stringWriter, new HtmlHelperOptions()); await viewResult.View.RenderAsync(viewContext); return(stringWriter.ToString()); } }
/// <summary> /// /// </summary> /// <param name="info"></param> /// <returns></returns> protected virtual RouteData GetRouteData(RouteInformation info) { var routeData = new RouteData(); if (!info.AreaName.IsEmpty()) { routeData.Values.Add("area", info.AreaName); } if (!info.ControllerName.IsEmpty()) { routeData.Values.Add("controller", info.ControllerName); } if (!info.ActionName.IsEmpty()) { routeData.Values.Add("action", info.ActionName); } if (info.IsPageRoute) { routeData.Values.Add("page", info.Path); } return(routeData); }
/// <summary> /// /// </summary> /// <param name="engine"></param> /// <param name="info"></param> /// <returns></returns> protected virtual ViewEngineResult GetView(ICompositeViewEngine engine, RouteInformation info) { return(engine.GetView("~/", $"~{info.Invocation}", !info.IsPartialView)); }
/// <summary> /// /// </summary> /// <param name="razorViewEngine"></param> /// <param name="actionContext">> /// <param name="info"></param> /// <returns></returns> protected virtual ViewEngineResult GetView(IRazorViewEngine razorViewEngine, ActionContext actionContext, RouteInformation info) { return(razorViewEngine.FindView(actionContext, info.ViewName.IsEmpty() ? info.ActionName : info.ViewName, !info.IsPartialView)); }