使用ASP.NET Core中的自定义Tag Helper将自定义标记替换为文件中的内容

时间:2018-03-31 22:06:29

标签: c# asp.net-core asp.net-core-mvc

我正在考虑创建一个自定义标记帮助,用外部文件中的内容替换原始标记,但我似乎无法在使用 ProcessAsync 时弄清楚如何做到这一点。

html

<body>
    <lc:default name="CSS" />
</body>

DefaultResourceTagHelper.cs

namespace LC.Tools.Utility.TagHelpers
{
    [HtmlTargetElement("lc:default", Attributes = "name", TagStructure = TagStructure.WithoutEndTag)]
    public class DefaultResourceTagHelper : TagHelperBase
    {
        public DefaultResourceTagHelper(IHostingEnvironment env) : base(env) { }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            try
            {
                WebRequest wr = WebRequest.Create(GetUrl());
                var hwr = await wr.GetResponseAsync();

                if (hwr.ContentLength > 0)
                {
                    using(Stream s = hwr.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            string temp = await sr.ReadToEndAsync();

                            context.Items.Clear();
                            context.Items.Add("test", temp);
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }

        private string GetUrl() {
            string result = "http://lctools.lundbeckconsulting.no/Resource/" + this.Version + "/Default";

            switch(this.Name)
            {
                case ResourceNames.CSS:
                    result += "CSS.txt";
                    break;

                case ResourceNames.Script:
                    result += "SCRIPT.txt";
                    break;
            }

            return result;
        }

        [HtmlAttributeName("name")]
        public ResourceNames Name { get; set; }

        [HtmlAttributeName("version")]
        public string Version { get; set; } = "Latest";
    }

    public enum ResourceNames
    {
        Script,
        CSS
    }
}

外部文件的文件内容

<link rel="icon" type="image/png" href="/IMAGES/fav-icon.png" />
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

我希望将<lc:default name="CSS" />标记替换为文件内容,最终结果如下:

<body>
    <link rel="icon" type="image/png" href="/IMAGES/fav-icon.png" />
    <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</body>

1 个答案:

答案 0 :(得分:0)

假设你从磁盘读取HTML文件(如果没有,那么只需将html行添加到列表html),然后按照:

 private void test()
{
list<string> html = new list<string>(File.ReadAllLines("htmlFIlePathHere"));
list<string> myFile = new list<string>(File.ReadAllLines("filepathhere"));
int index;
string foundWord = "";
foreach (var ite in htmk)
{
    if (ite.Contains("<lc:default"))
    {
        index = html.FindIndex(a => a == ite);
        foundWord = ite;
    }
}

html(index).Replace(foundWord, "");
foreach (var item in myFile)
{
    html(index) = item;
    index = index + 1;
}
html.Add("<body>")
html.Add("</body>")

foreach (var item in html)
{
    MsgBox(item); /// u have a list of strings or should i say u have your  result, use it the way you want :)
}
}