我终于让它为我工作了。我已对您在以下位置创建的问题添加了评论https://github.com/dotnet/AspNetCore.Docs/issues/16837 https://github.com/dotnet/AspNetCore.Docs/issues/16837.
我现在工作的 .csproj 的相关部分是:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<GenerateMvcApplicationPartsAssemblyAttributes>true</GenerateMvcApplicationPartsAssemblyAttributes>
<RazorCompileOnBuild>true</RazorCompileOnBuild>
<IncludeRazorContentInPack>false</IncludeRazorContentInPack>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<EnableDefaultRazorGenerateItems>true</EnableDefaultRazorGenerateItems>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
</ItemGroup>
<PropertyGroup>
<StaticWebAssetBasePath Condition="$(StaticWebAssetBasePath) == ''">/</StaticWebAssetBasePath>
</PropertyGroup>
</Project>
也许并不是上面所有的设置都是必需的,但由于它正在工作,我不想过多地接触它 -如果有效,请勿触摸 :-)
现在,还有其他一些阻碍的问题:
- 因为我想在本地测试我的 NuGet 包,所以我想首先将包推送到本地存储库文件夹,以便我可以从测试应用程序使用它们。然而,没有任何地方记录 dotnet nuget 也可以推送到本地目录,所以我想我必须使用独立的 nuget.exe 来实现这一点。嗯,显然将 nuget.exe 与 RCL 库一起使用不是一个好主意 https://github.com/dotnet/AspNetCore.Docs/issues/13951。相反,您可以使用:
dotnet nuget push .\bin\Release\DynamicVML.1.0.32.nupkg -s c:\Projects\nuget
(请注意,-s 选项被描述为server URL
in the 文档 https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push但其实也可以是本地文件夹,这个就不解释了)
有太多过时的文章和 StackOverflow 答案可能会让您走上错误的道路。我身上发生的事情是,在某个时刻我试图跟随教程 https://www.learnrazorpages.com/advanced/razor-class-library from 不同的 https://dotnetstories.com/blog/How-to-Include-static-files-in-a-Razor-library-en-7156675136 sources https://weblog.west-wind.com/posts/2019/Sep/30/Serving-ASPNET-Core-Web-Content-from-External-Folders事实证明,它对于 .NET Core 3.1 来说已经过时了。我最终弄乱了我的项目文件,并忘记撤消我所做的一些更改。为了使其一切正常,请确保:
视图/.cshtml 文件必须设置为“Build Action: Content”;
- 静态文件/js文件必须设置为“Build Action: Content”;
现在,到消费者应用程序部分:
- 我不必添加
webBuilder.UseStaticWebAssets();
在 Program.cs 中,有一瞬间我以为我已经做到了。所以我的 CreateHostBuilder 看起来就像这样:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
我不必添加过时教程中描述的任何 FileProvider 功能。我的配置服务就像这样干净:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("BookAuthors"));
}
我的配置只是基线配置:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
通过以上设置,一切正常。我可以包含 RCL 库中的 js 文件即使他们实际上并不存在于消费者项目 wwwroot 中(我知道当前文档中记录了这一点,但在某些时候我试图通过添加使用DirectoryBrowser进行我的配置 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1并检查所提供文件夹的内容 - 这不起作用,来自 RCL 的静态文件将不会显示)。
另外,请注意其他有类似问题的人:通过上述设置(更具体地说,由于最后一个配置块),我可以使用来自的静态文件/
。我没有使用/_content/LibraryName/...
路径。在我的 RCL 中,我有以下结构
- wwwroot
---- lib
------- myFolder
---------- myScript.js
在消费者应用程序中,我只需使用以下命令即可使用 .js 文件
@section Scripts {
<script src="~/lib/myFolder/myScript.js"></script>
}
...而且它有效!