Paketprojektstruktur:
Code: Select all
TagHelperNuGet/
├── wwwroot/
│ ├── css/
│ │ └── style.cs
│ └── js/
│ └── script.js
├── TagHelpers/
│ └── BtnTagHelper.cs
└── Extensions/
└── StaticFileExtension.cs
Wann Ich versuche, den Namensraum in meine Program.cs-Datei
zu importieren
Code: Select all
using RazorClassLibraryTest.Extensions;
Code: Select all
The type or namespace name 'Extensions' does not exist in the namespace 'RazorClassLibraryTest' (are you missing an assembly reference?)
Code: Select all
using RazorClassLibraryTest.TagHelpers;
Code: Select all
AlertBtnTagHleper.cs
Code: Select all
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace RazorClassLibraryTest.TagHelpers
{
[HtmlTargetElement("alert-btn")]
public class AlertBtnTagHelper : TagHelper
{
[HtmlAttributeName("text")]
public string Text { get; set; } = "Show Alert";
[HtmlAttributeName("message")]
public string Message { get; set; } = "Hello World!";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "button";
output.Attributes.SetAttribute("type", "button");
output.Attributes.SetAttribute("class", "btn-alert");
output.Attributes.SetAttribute("onclick", $"showAlert('{Message}')");
output.Content.SetContent(Text);
}
}
}
Code: Select all
StaticFilesExtention.cs
Code: Select all
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using System.Reflection;
namespace RazorClassLibraryTest.Extensions
{
public static class StaticFilesExtension
{
///
/// Configures the application to serve static files from a specified staticFilesRoot path.
///
/// The application builder.
/// The staticFilesRoot path to serve static files from. Defaults to _content/RazorClassLibraryTest/.
/// The application builder.
public static IApplicationBuilder UseTagStatics(this IApplicationBuilder app, string? staticFilesRoot)
{
if (string.IsNullOrWhiteSpace(staticFilesRoot))
return app;
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments(staticFilesRoot))
{
Assembly assembly = Assembly.GetAssembly(typeof(StaticFilesExtension))!;
string? assemblyName = assembly.GetName().Name;
if (string.IsNullOrWhiteSpace(assemblyName))
throw new InvalidOperationException("The assembly name could not be determined.");
string assetPath = context.Request.Path.Value[(staticFilesRoot.Length + 1)..];
context.Request.Path = $"/_content/{assemblyName}/{assetPath}";
}
await next();
});
return app;
}
///
/// Configures the application to serve static files from a path specified in the configuration.
///
/// The application builder.
/// The configuration to get the staticFilesRoot path from. Expects the value to be stored in "RazorTag.StaticFilesRoot".
/// The application builder.
public static IApplicationBuilder UseTagStatics(this IApplicationBuilder app, IConfiguration configuration)
{
string? configStaticFilesRoot = configuration["RazorTag:StaticFilesRoot"];
if (string.IsNullOrWhiteSpace(configStaticFilesRoot))
return app;
return app.UseTagStatics(configStaticFilesRoot);
}
}
}
Bearbeiten:
Die Erweiterungen sind verpackt, aber sie funktionieren immer noch nicht
Bild der Paketstruktur mit der DotPeek-Anwendung