【发布时间】:2017-05-18 23:21:38
【问题描述】:
我试图提供外部库中的静态文件。
我已经完成了控制器和视图的工作,但我无法从该库中加载资源(javascript、图像...)。
这是我的 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//...
var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
var personEmbeddedFileProvider = new EmbeddedFileProvider(
personAssembly,
"PersonComponent"
);
services
.AddMvc()
.AddApplicationPart(personAssembly)
.AddControllersAsServices();
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Add(personEmbeddedFileProvider);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
var personEmbeddedFileProvider = new EmbeddedFileProvider(
personAssembly,
"PersonComponent"
);
app.UseStaticFiles();
//This not work
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new CompositeFileProvider(
personEmbeddedFileProvider
)
});
}
这里是我在外部库的 project.json 上的 buildOptions 设置:
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"embed": [
"Views/**/*.cshtml",
"wwwroot/**"
]
},
谁能告诉我怎么了?
谢谢大家(对不起我的英语不好)
【问题讨论】:
-
可能是因为您的资源位于 wwwroot 文件夹中。尝试将其内容移动到外部库的根目录
-
这对我有用!谢谢!但我仍在寻找替代方案而不将内容移动到根目录,因为我想将 Bower 用于外部插件。我再次发布的任何内容
-
我认为不可能,无需编写您自己的提供程序。
EmbeddedFileProvider似乎不支持看起来像github.com/aspnet/FileSystem/blob/rel/1.1.0/src/… 的根路径 这个根路径是为PhysicalFileProvider设置为wwwroot,因为请求中间件不知道文件所在的位置。使用中间件时,只有不带wwwroot的路径会传递给中间件。 -
你可以尝试使用命名空间重载的构造函数重载,但我怀疑它会起作用并且可能会破坏视图
-
无论如何将包存储在类库中也是一个坏主意。通常,您在部署后或创建 docker 容器时使用 npm 或 bower 来恢复它
标签: c# .net asp.net-core asp.net-core-mvc .net-core