angular - 角度 netcore 应用程序重定向到 /index.html 而不是 /
问题描述
我有一个 Angular 7 + Asp.Net Core 2.2 网站。
当用户输入 url:domain.com/
他们被重定向到domain.com/index.html
,
然后,当 Angular 应用程序启动时,url 会返回到domain.com/
.
如何防止跳转到 index.html 并使其看起来像这样:domain.com/
从头到尾?
我将用户重定向到角度应用程序的方式是 Startup.cs 类中的这段代码(注意我使用/
and not /index.html
,它仍然重定向到/.index.html
):
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value) &&
!context.Request.Path.Value.StartsWith("/api/") &&
{
context.Request.Path = "/";
await next();
}
});
解决方案
这可能会或可能不会解决您的问题,但我也遇到了同样的问题。对我来说,这是由标准的 swagger generation 中间件引起的Startup.cs
:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", $"{nameof(SharingService)} API V1");
c.RoutePrefix = "";
});
此中间件导致从/
to重定向/index.html
以显示 swagger api。
为了解决这个问题,我在之前添加了包括使用静态文件和使用默认文件的内容。这会提供文件内容,而无需先引导浏览器。app.UseFileServer()
app.UseSwagger()
我还更新了要设置的 swagger 配置,c.RoutePrefix = "api"
以便 swagger 页面在/api
(而不是 root)可用。
推荐阅读
- elasticsearch - 为什么 elasticsearch 引入 _primary_term 来获得乐观并发控制?
- css - 需要表格在 Next Row 组件上溢出
- ios - iOS 人脸识别 - 可变数据集
- csv - CSVMapper 创建带有标题的文件,然后附加相同的没有标题的文件
- javascript - 没有错误的损坏的 JavaScript 程序
- http - 如何找出 API 所在的位置?
- python - Python`从模块导入名称`与`导入模块`
- microsoft-graph-api - 使用 Graph API 创建 AD 组时出现问题
- java - 改变变量,取决于滚轮
- python - 根据键值附加列表列表