I'm looking into creating a *custom tag helper* that replaces the original tag with content from an *external file*, but I can't seem to figure out how to do it when using *ProcessAsync*.
The 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
}
}
The file content of the external file:<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">
I'm looking to replace the <lc:default name="CSS" />
tag with the file content, with the end result like this:
<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>