首頁 > 關注 > > 正文

Asp.net core使用Razor試圖引擎編寫TagHelper-環球看點

2023-06-27 20:20:49    來源:博客園

之前使用wtm來進行快速開發


(相關資料圖)

wtm框架見:

https://wtmdoc.walkingtec.cn/

其前端選擇Layui的情況下有大量的TagHelper,大幅度提高了開發效率

雖然已有的組件很豐富但也不能完全覆蓋所有的需求,這個時候就需要自己寫TagHelper。

參考了WTM源碼,和微軟官方文檔

TagHelper雖然使用起來方便,但是大量的拼接字符串編寫體驗和可讀性都不是很好。

理想的情況是能充分利用.net中強大的Razor引擎來編寫TagHelper,從而更方便的進行組件復用。

可以從asp.net core中找到viewengine的使用方法

以封裝一個wangEditor為例

TagHelper:

using Microsoft.AspNetCore.Html;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc.ModelBinding;using Microsoft.AspNetCore.Mvc.Rendering;using Microsoft.AspNetCore.Mvc.ViewEngines;using Microsoft.AspNetCore.Mvc.ViewFeatures;using Microsoft.AspNetCore.Razor.TagHelpers;using System;using System.IO;using WalkingTec.Mvvm.Core;using WalkingTec.Mvvm.Core.Extensions;using WalkingTec.Mvvm.TagHelpers.LayUI;namespace TagHelpers{    [HtmlTargetElement("wt:wangeditor", Attributes = "field", TagStructure = TagStructure.WithoutEndTag)]    public class WangEditorTagHelper : TagHelper    {        public WangEditorTagHelper(ICompositeViewEngine viewEngine,IHttpContextAccessor httpContextAccessor)         {            _viewEngine = viewEngine;            _httpContextAccessor = httpContextAccessor;        }        public ModelExpression Field { get; set; }        public string Id { get; set; }        public string Name { get; set; }        public int Height { get; set; } = 300;        private ICompositeViewEngine _viewEngine;        private IHttpContextAccessor _httpContextAccessor;        public override void Process(TagHelperContext context, TagHelperOutput output)        {            var viewEngineResult = _viewEngine.GetView("~/Views/Shared/Components/WangEditor/", "Default.cshtml", false);            if (!viewEngineResult.Success)            {                throw new InvalidOperationException($"Couldn"t find view /Shared/Components/WangEditor/Default.cshtml");            }            using (var sr = new StringWriter())            {                var viewContext = new ViewContext();                viewContext.HttpContext = _httpContextAccessor.HttpContext;                viewContext.ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())                {                    Model = new WangEditorConfig()                    {                        Id = Id ?? Utils.GetIdByName(Field?.ModelExplorer.Container.ModelType.Name + "." + Field?.Name),                        Name = Name ?? Field?.Name,                        Value = Field?.Model?.ToString(),                        Height = Height                    }                };                viewContext.Writer = sr;                viewEngineResult.View.RenderAsync(viewContext).GetAwaiter().GetResult();                output.TagName = "div";                output.TagMode = TagMode.StartTagAndEndTag;                output.Content.SetHtmlContent(sr.ToString());            }        }    }    public class WangEditorConfig    {        public string Id { get; set; }        public string Name { get; set; }        public string Value { get; set; }        public int Height { get; set; }    }}

cshtml,使用razor視圖引擎編寫可讀性就好了很多。

@using TagHelpers;@model WangEditorConfig

關鍵詞:

上一篇:敬禮娃娃高考斬獲好成績 當前頭條
下一篇:最后一頁

熱點話題

熱點推薦

頭條

?