我终于让它为我工作了。我已对您在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 库一起使用并不是一个好主意。相反,您可以使用:
dotnet nuget push .\bin\Release\DynamicVML.1.0.32.nupkg -s c:\Projects\nuget
(注意 -s 选项server URL
在文档中被描述为 a但它实际上可以是本地文件夹,这里没有解释)
现在,到消费者应用程序部分:
- 我不需要
webBuilder.UseStaticWebAssets();
像我想的那样添加 Program.cs。所以我的 CreateHostBuilder 看起来像这样:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
我不必添加过时教程中描述的任何 FileProvider 功能。我的 ConfigureServices 就像这样干净:
// 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中(我知道这已记录在当前文档中,但在某些时候我试图通过将UseDirectoryBrowser 添加到我的配置和检查服务文件夹的内容 - 这不起作用,来自 RCL 的静态文件不会显示)。
另外,对于其他有类似问题的人的说明:使用上述设置(由于最后一个配置块,更具体地说)我可以使用来自/
. 我没有使用/_content/LibraryName/...
路径。在我的 RCL 中,我有以下结构
- wwwroot
---- lib
------- myFolder
---------- myScript.js
从消费者应用程序中,我只需使用 .js 文件
@section Scripts {
<script src="~/lib/myFolder/myScript.js"></script>
}
......它的工作原理!