我试图提供外部库内的静态文件。
我已经完成了控制器和视图的工作,但我无法从该库加载资源(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”.
假设您的程序集名称是人物组件
var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
var personEmbeddedFileProvider = new EmbeddedFileProvider(
personAssembly,
"PersonComponent.wwwroot"
);
然后你必须使用这个文件提供程序使用静态文件 call
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = personEmbeddedFileProvider
});
您可以选择使用替代内容请求路径,这样您就可以使用不同的 URL 获取本地和外部资源。
为此,只需填写RequestPath
创建时变量StaticFileOptions
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = personEmbeddedFileProvider,
RequestPath = new PathString("/external")
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)