Skip to content

Commit 277a9ba

Browse files
committed
Use JavaScriptLoggingTagHelperComponent
1 parent 926fb2f commit 277a9ba

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

jsLogger/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Logging;
66
using Microsoft.AspNetCore.Routing;
77
using jsLogger.Extensions;
8+
using Microsoft.AspNetCore.Razor.TagHelpers;
9+
using jsLogger.TagHelpers;
810

911
namespace jsLogger
1012
{
@@ -30,6 +32,8 @@ public void ConfigureServices(IServiceCollection services)
3032
{
3133
options.HandleGlobalExceptions = true;
3234
});
35+
36+
services.AddSingleton<ITagHelperComponent, JavaScriptLoggingTagHelperComponent>();
3337
}
3438

3539
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System;
3+
using System.Threading.Tasks;
4+
5+
namespace jsLogger.TagHelpers
6+
{
7+
public class JavaScriptLoggingTagHelperComponent : TagHelperComponent
8+
{
9+
private readonly string _script;
10+
11+
public JavaScriptLoggingTagHelperComponent(JavaScriptLoggingSnippet jsLoggingSnippet)
12+
{
13+
_script = jsLoggingSnippet.Script;
14+
}
15+
16+
public override int Order => 1;
17+
18+
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
19+
{
20+
if (string.Equals(context.TagName, "head", StringComparison.Ordinal))
21+
{
22+
output.PostContent.AppendHtml(_script);
23+
}
24+
25+
return Task.CompletedTask;
26+
}
27+
}
28+
}

jsLogger/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
@inject JavaScriptLoggingSnippet JavaScriptLoggingSnippet
2-
3-
<!DOCTYPE html>
1+
<!DOCTYPE html>
42
<html>
53
<head>
64
<meta charset="utf-8" />
@@ -17,7 +15,6 @@
1715
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
1816
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
1917
</environment>
20-
@Html.Raw(JavaScriptLoggingSnippet.Script)
2118
</head>
2219
<body>
2320
<nav class="navbar navbar-inverse navbar-fixed-top">

0 commit comments

Comments
 (0)