当前位置: 首页 > news >正文

汝州网站制作软件开发工作岗位

汝州网站制作,软件开发工作岗位,网络公司推广方案,网站建设服务器租赁一、前言最近做项目的时候#xff0c;使用Util进行开发#xff0c;使用Razor写前端页面。初次使用感觉还是不大习惯#xff0c;之前都是前后端分离的方式开发的#xff0c;但是使用Util封装后的Angular后#xff0c;感觉开发效率还是杠杠滴。二、问题在发布代码的时候使用Util进行开发使用Razor写前端页面。初次使用感觉还是不大习惯之前都是前后端分离的方式开发的但是使用Util封装后的Angular后感觉开发效率还是杠杠滴。二、问题在发布代码的时候Webpack打包异常提示是缺少了某些Html文件我看了下相应的目录发现目录缺少了部分Html文件然后就问了何镇汐大大给出的解决方案是每个页面都需要访问一下才能生成相应的Html静态文件。这时候就产生了疑虑是否有一种方式能获取所有路由然后只需访问一次即可生成所有的Html页面。三、解决方案3.1 每次访问生成Html解决方案思路继承ActionFilterAttribute特性重写执行方法访问的时候判断访问的Result是否ViewResult如果是方可生成Html从RazorViewEngine中查找到View后进行渲染/// summary/// 生成Html静态文件/// /summarypublic 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 Taskstring 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.CreateIRazorViewEngine();        var tempDataProvider Ioc.CreateITempDataProvider();        var serviceProvider Ioc.CreateIServiceProvider();        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/returnspublic IEnumerableRouteInformation GetAllRouteInformations(){    ListRouteInformation list new ListRouteInformation();    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.GetCustomAttributeRazorHtmlAttribute();            if (controllerHtmlAttribute ! null)            {                info.FilePath controllerHtmlAttribute.Path;                info.TemplatePath controllerHtmlAttribute.Template;            }            var htmlAttribute controllerActionDescriptor.MethodInfo.GetCustomAttributeRazorHtmlAttribute();            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/returnspublic async Task Generate(){    foreach (var routeInformation in _routeAnalyzer.GetAllRouteInformations())    {        // 跳过API的处理        if (routeInformation.Path.StartsWith(/api))        {            continue;        }        await WriteViewToFileAsync(routeInformation);    }}/// summary/// 渲染视图为字符串/// /summary/// param nameinfo路由信息/param/// returns/returnspublic async Taskstring RenderToStringAsync(RouteInformation info){    var razorViewEngine Ioc.CreateIRazorViewEngine();    var tempDataProvider Ioc.CreateITempDataProvider();    var serviceProvider Ioc.CreateIServiceProvider();    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 nameinfo路由信息/param/// returns/returnspublic 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/UtilBing.NetCore: https://github.com/bing-framework/Bing.NetCoreRazor生成静态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
http://www.yutouwan.com/news/240819/

相关文章:

  • 我们的网站企业年检网上申报流程
  • 南昌建站费用江宁做网站
  • 深圳网站开发的公司电话迁安建设局官方网站
  • 网站开发竞争对手分析怎么上传网站模板
  • 电力行业网站建设网站开发项目团队人员
  • 东莞网站案例营销做体育的网站
  • 空间备案和网站备案网站上地图是怎样做的
  • 做单网站企业网站建设开发成本利润多少
  • 上海哪家公司做网站好千库网网页版登录官网
  • 网站建设方维商标查询网入口
  • 贵州景点网站建设方案西安建设银行工作招聘网站
  • 网站建设板块如何分类企业信用公示信息网官网贵州
  • 网站后台管理系统 源码wordpress评论代码
  • 微信的公众平台网站开发.net做的网站怎么样
  • 广州网络服装网站建设音平商城谁做的网站
  • 好站站网站建设推广苏州网站建设营销推广
  • 温州做网站多少钱装修公司加盟条件
  • 长春建站网站建设x站源码免费分享
  • 互动网站网站刷新代码
  • 学校html网站模板代码网站建设培训简报
  • 网站提供服务商南京企业微信网站建设
  • 商务网站建设试卷软文推广经典案例
  • 河北建设厅网站查询wordpress仿简书
  • 网站案例展示怎么做企业展厅设计图片欣赏
  • 网站开发 学习步骤西安保安公司
  • 合肥高端网站开发公司网站 跳出率 多少
  • 大型网站建设建设公司排名手机交互网站
  • 做网站怎么招广告百度发作品入口在哪里
  • 前几年做那个网站能致富企业网络推广如何做
  • 官网模板建站塔山双喜哪里可以免费发布招聘信息