日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET Core Razor生成Html静态文件

發布時間:2023/12/4 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET Core Razor生成Html静态文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、前言

最近做項目的時候,使用Util進行開發,使用Razor寫前端頁面。初次使用感覺還是不大習慣,之前都是前后端分離的方式開發的,但是使用Util封裝后的Angular后,感覺開發效率還是杠杠滴。

二、問題

在發布代碼的時候,Webpack打包異常,提示是缺少了某些Html文件,我看了下相應的目錄,發現目錄缺少了部分Html文件,然后就問了何鎮汐大大,給出的解決方案是,每個頁面都需要訪問一下才能生成相應的Html靜態文件。這時候就產生了疑慮,是否有一種方式能獲取所有路由,然后只需訪問一次即可生成所有的Html頁面。

三、解決方案

3.1 每次訪問生成Html

解決方案思路:

  • 繼承ActionFilterAttribute特性,重寫執行方法

  • 訪問的時候判斷訪問的Result是否ViewResult,如果是方可生成Html

  • 從RazorViewEngine中查找到View后進行渲染

/// <summary>

/// 生成Html靜態文件

/// </summary>

public class HtmlAttribute : ActionFilterAttribute {

? ? /// <summary>

? ? /// 生成路徑,相對根路徑,范例:/Typings/app/app.component.html

? ? /// </summary>

? ? public string Path { get; set; }


? ? /// <summary>

? ? /// 路徑模板,范例:Typings/app/{area}/{controller}/{controller}-{action}.component.html

? ? /// </summary>

? ? public string Template { get; set; }


? ? /// <summary>

? ? /// 執行生成

? ? /// </summary>

? ? public override async Task OnResultExecutionAsync( ResultExecutingContext context, ResultExecutionDelegate next ) {

? ? ? ? await WriteViewToFileAsync( context );

? ? ? ? await base.OnResultExecutionAsync( context, next );

? ? }


? ? /// <summary>

? ? /// 將視圖寫入html文件

? ? /// </summary>

? ? private async Task WriteViewToFileAsync( ResultExecutingContext context ) {

? ? ? ? try {

? ? ? ? ? ? var html = await RenderToStringAsync( context );

? ? ? ? ? ? if( string.IsNullOrWhiteSpace( html ) )

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? var path = Util.Helpers.Common.GetPhysicalPath( string.IsNullOrWhiteSpace( Path ) ? GetPath( context ) : Path );

? ? ? ? ? ? var directory = System.IO.Path.GetDirectoryName( path );

? ? ? ? ? ? if( string.IsNullOrWhiteSpace( directory ) )

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? if( Directory.Exists( directory ) == false )

? ? ? ? ? ? ? ? Directory.CreateDirectory( directory );

? ? ? ? ? ? File.WriteAllText( path, html );

? ? ? ? }

? ? ? ? catch( Exception ex ) {

? ? ? ? ? ? ex.Log( Log.GetLog().Caption( "生成html靜態文件失敗" ) );

? ? ? ? }

? ? }


? ? /// <summary>

? ? /// 渲染視圖

? ? /// </summary>

? ? protected async Task<string> RenderToStringAsync( ResultExecutingContext context ) {

? ? ? ? string viewName = "";

? ? ? ? object model = null;

? ? ? ? if( context.Result is ViewResult result ) {

? ? ? ? ? ? viewName = result.ViewName;

? ? ? ? ? ? viewName = string.IsNullOrWhiteSpace( viewName ) ? context.RouteData.Values["action"].SafeString() : viewName;

? ? ? ? ? ? model = result.Model;

? ? ? ? }

? ? ? ? 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, context.RouteData, new ActionDescriptor() );

? ? ? ? using( var stringWriter = new StringWriter() ) {

? ? ? ? ? ? var viewResult = razorViewEngine.FindView( actionContext, viewName, true );

? ? ? ? ? ? if( viewResult.View == null )

? ? ? ? ? ? ? ? throw new ArgumentNullException( $"未找到視圖: {viewName}" );

? ? ? ? ? ? var viewDictionary = new ViewDataDictionary( new EmptyModelMetadataProvider(), new ModelStateDictionary() ) { Model = model };

? ? ? ? ? ? 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>

? ? /// 獲取Html默認生成路徑

? ? /// </summary>

? ? protected virtual string GetPath( ResultExecutingContext context ) {

? ? ? ? var area = context.RouteData.Values["area"].SafeString();

? ? ? ? var controller = context.RouteData.Values["controller"].SafeString();

? ? ? ? var action = context.RouteData.Values["action"].SafeString();

? ? ? ? var path = Template.Replace( "{area}", area ).Replace( "{controller}", controller ).Replace( "{action}", action );

? ? ? ? return path.ToLower();

? ? }

}

3.2 一次訪問生成所有Html

解決方案思路:

  • 獲取所有已注冊的路由

  • 獲取使用RazorHtml自定義特性的路由

  • 忽略Api接口的路由

  • 構建RouteData信息,用于在RazorViewEngine中查找到相應的視圖

  • 構建ViewContext用于渲染出Html字符串

  • 將渲染得到的Html字符串寫入文件

獲取所有注冊的路由,此處是比較重要的,其他地方也可以用到。

/// <summary>

/// 獲取所有路由信息

/// </summary>

/// <returns></returns>

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"];

? ? ? ? }


? ? ? ? // Razor頁面路徑以及調用

? ? ? ? if (actionDescriptor is PageActionDescriptor pageActionDescriptor)

? ? ? ? {

? ? ? ? ? ? info.Path = pageActionDescriptor.ViewEnginePath;

? ? ? ? ? ? info.Invocation = pageActionDescriptor.RelativePath;

? ? ? ? }


? ? ? ? // 路由屬性路徑

? ? ? ? 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}";

? ? ? ? ? ? }


? ? ? ? ? ? var controllerHtmlAttribute = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<RazorHtmlAttribute>();


? ? ? ? ? ? if (controllerHtmlAttribute != null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? info.FilePath = controllerHtmlAttribute.Path;

? ? ? ? ? ? ? ? info.TemplatePath = controllerHtmlAttribute.Template;

? ? ? ? ? ? }


? ? ? ? ? ? var htmlAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttribute<RazorHtmlAttribute>();


? ? ? ? ? ? if (htmlAttribute != null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? info.FilePath = htmlAttribute.Path;

? ? ? ? ? ? ? ? info.TemplatePath = htmlAttribute.Template;

? ? ? ? ? ? }


? ? ? ? ? ? info.ControllerName = controllerActionDescriptor.ControllerName;

? ? ? ? ? ? info.ActionName = controllerActionDescriptor.ActionName;

? ? ? ? ? ? info.Invocation = $"{controllerActionDescriptor.ControllerName}Controller.{controllerActionDescriptor.ActionName}";

? ? ? ? }


? ? ? ? info.Invocation += $"({actionDescriptor.DisplayName})";


? ? ? ? list.Add(info);

? ? }


? ? return list;

}

生成Html靜態文件

/// <summary>

/// 生成Html文件

/// </summary>

/// <returns></returns>

public async Task Generate()

{

? ? foreach (var routeInformation in _routeAnalyzer.GetAllRouteInformations())

? ? {

? ? ? ? // 跳過API的處理

? ? ? ? if (routeInformation.Path.StartsWith("/api"))

? ? ? ? {

? ? ? ? ? ? continue;

? ? ? ? }

? ? ? ? await WriteViewToFileAsync(routeInformation);

? ? }

}


/// <summary>

/// 渲染視圖為字符串

/// </summary>

/// <param name="info">路由信息</param>

/// <returns></returns>

public async Task<string> RenderToStringAsync(RouteInformation info)

{

? ? var razorViewEngine = Ioc.Create<IRazorViewEngine>();

? ? var tempDataProvider = Ioc.Create<ITempDataProvider>();

? ? var serviceProvider = Ioc.Create<IServiceProvider>();


? ? 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);

? ? }


? ? var httpContext = new DefaultHttpContext { RequestServices = serviceProvider };

? ? var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());


? ? var viewResult = razorViewEngine.FindView(actionContext, info.ActionName, true);

? ? 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>

public async Task WriteViewToFileAsync(RouteInformation info)

{

? ? try

? ? {

? ? ? ? var html = await RenderToStringAsync(info);

? ? ? ? if (string.IsNullOrWhiteSpace(html))

? ? ? ? ? ? return;


? ? ? ? var path = Utils.Helpers.Common.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);

? ? ? ? File.WriteAllText(path, html);

? ? }

? ? catch (Exception ex)

? ? {

? ? ? ? ex.Log(Log.GetLog().Caption("生成html靜態文件失敗"));

? ? }

}


protected virtual string GetPath(RouteInformation info)

{

? ? 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();

}

四、使用方式

  • MVC控制器配置

  • Startup配置

  • 一次性生成方式,調用一次接口即可


五、源碼地址

Util:?https://github.com/dotnetcore/Util

Bing.NetCore:?https://github.com/bing-framework/Bing.NetCore

Razor生成靜態Html文件:https://github.com/dotnetcore/Util/tree/master/src/Util.Webs/Razors?或者?https://github.com/bing-framework/Bing.NetCore/tree/master/src/Bing.Webs/Razors


六、參考

獲取所有已注冊的路由:https://github.com/kobake/AspNetCore.RouteAnalyzer

原文地址: https://www.cnblogs.com/jianxuanbing/p/9183359.html


.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結

以上是生活随笔為你收集整理的ASP.NET Core Razor生成Html静态文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。