c# - ASP.NET Core UseFileServer 并在 404 上重定向
问题描述
我正在使用以下代码将 SPA 嵌入到 C# 库中,以便在 ASP.NET Core 应用程序中使用。
public static IApplicationBuilder UseSharedUI(this IApplicationBuilder app)
{
app.UseFileServer(new FileServerOptions
{
RequestPath = "/shared-ui",
FileProvider = new ManifestEmbeddedFileProvider(assembly: Assembly.GetAssembly(typeof(Builder)), "UI/dist"),
});
return app;
}
这有效,当我运行 API 时,我可以访问/shared-ui
路由下的 SPA。但是当我在浏览器中的路由中进行刷新时/shared-ui/analytics
,我收到 404 错误,因为应用程序不知道如何处理子路由,它应该回退到 SPA 来处理它。
我尝试添加以下代码,但行为是相同的。
app.MapWhen((context) => context.Request.Path.StartsWithSegments("/shared-ui"), (appBuilder) =>
{
app.UseStaticFiles();
appBuilder.UseRouting();
appBuilder.UseEndpoints(endpoints =>
{
endpoints.MapFallbackToFile("/shared-ui/index.html");
});
});
有什么办法可以做到这一点?
解决方案
推荐阅读
- microsoft-graph-api - Microsoft Graph Mail API 中的过滤器和搜索查询参数有什么区别?
- ios - 更新 UITableView 页脚而不丢失部分中的文本字段
- python - 如何在python中使用散点图在其周边创建一个具有均匀分布点的圆
- linux - 从 Linux 用户空间控制 FlashAir SD 卡
- python - Python3 无法将 str 转换为 dict 或 json
- c++ - 映射 C++ 按值排序和插入元素
- hadoop - Hadoop:不推荐使用 dfs 但未找到 hdfs 类
- html-table - 具有实时处理角度 5 的表
- c# - VSTS 无法加载源 401 的服务索引
- ios - 有没有比使用 CocoaAsyncSocket 更好的方法通过互联网通过 P2P 发送图像?