Mein ASP-Projekt kann nicht auf Klassen aus einem von mir erstellten NuGet-Paket zugreifen

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Mein ASP-Projekt kann nicht auf Klassen aus einem von mir erstellten NuGet-Paket zugreifen

by Guest » 16 Jan 2025, 11:36

Also experimentiere ich mit der Erstellung von NuGet-Paketen und dieses hat einen Tag-Helper und eine Erweiterungsmethode für den IApplicationBuilder, ich habe das Paket gepackt und in einem lokalen Ordner abgelegt und den Ordner zu den Paketquellen hinzugefügt Anschließend wurde das Paket installiert.
Paketprojektstruktur:

Code: Select all

TagHelperNuGet/
├── wwwroot/
│   ├── css/
│   │   └── style.cs
│   └── js/
│       └── script.js
├── TagHelpers/
│   └── BtnTagHelper.cs
└── Extensions/
└── StaticFileExtension.cs
Der Tag-Helfer wird erkannt (nach dem Hinzufügen von @addTagHelper *, RazorClassLibraryTest ofc), die Erweiterungsmethode jedoch nicht.
Wann Ich versuche, den Namensraum in meine Program.cs-Datei
zu importieren

Code: Select all

using RazorClassLibraryTest.Extensions;
Ich bekomme

Code: Select all

The type or namespace name 'Extensions' does not exist in the namespace 'RazorClassLibraryTest' (are you missing an assembly reference?)
Aber wenn ich dasselbe für den Tag-Helfer-Namensraum mache, wird er ohne Probleme importiert

Code: Select all

using RazorClassLibraryTest.TagHelpers;
Dies ist der Code beider Dateien:

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);
}
}
}
Codeüberprüfungen und Verbesserungsvorschläge sind willkommen und vielen Dank.
Bearbeiten:
Die Erweiterungen sind verpackt, aber sie funktionieren immer noch nicht
Bild der Paketstruktur mit der DotPeek-Anwendung

Top