ASP.NET Core 和 Angular 部署 - 页面刷新出现 404 错误

发布于 2025-01-16 08:09:27 字数 517 浏览 0 评论 0原文

我正在开发一个在 Plesk 上托管的 Angular 和 ASP.NET Core 项目。该项目工作正常,只是页面刷新产生 404 页面未找到错误。每当我从浏览器中点击刷新或重新加载页面时,都会出现 404 错误。

文件的结构如下图所示。

web.config 中的内容为:

在“wwwroot”文件夹中,我有来自 Angular 的构建,我在其中创建了一个包含以下内容的“web.config”文件:

此外,在“wwwroot”文件夹中,我还有包含以下内容的“index.html”文件:

请指导我解决这个问题。

谢谢!

I am working on a project in Angular and ASP.NET Core hosted on Plesk. The project is working fine except the page refresh yields the 404 page not found error. Whenever I hit refresh from the browser or reload the page, it gives the 404 error.

The structure of the files it's in the picture below.

The content in web.config is:

In the "wwwroot" folder I have the build from Angular, where I created a "web.config" file with the following content:

Also in the "wwwroot" folder, I have the "index.html" file with the following content:

Please guide me to solve this issue.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不美如何 2025-01-23 08:09:27

昨晚我遇到了同样的问题,但使用的是后端使用 ASP .net 6.0 的 Vue 应用程序。
此处很好地解释了错误的原因,但我使用不同的解决方案来解决这个问题。

我的解决方案是在program.cs中添加一个MapFallbackTocontroller控制器:

app.MapControllers();
app.MapFallbackToController("Index", "Fallback");

await app.RunAsync();

然后创建所述FallbackController.cs:

[AllowAnonymous]
public class FallbackController : Controller
{
   public IActionResult Index()
   {
      return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/html");
   }
}

I ran in the same problem last night, but with a Vue application that uses ASP .net 6.0 in the backend.
The reason of the error is well explained here, but I used a different solution to solve this problem.

My solution was adding a MapFallbackTocontroller controller in program.cs:

app.MapControllers();
app.MapFallbackToController("Index", "Fallback");

await app.RunAsync();

and then creating said FallbackController.cs:

[AllowAnonymous]
public class FallbackController : Controller
{
   public IActionResult Index()
   {
      return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/html");
   }
}
抹茶夏天i‖ 2025-01-23 08:09:27

Net 6 和 Angular Spa 应用程序刷新 404 问题出现。随着索引 html 更改,在 program/startup.cs 中添加以下行,刷新问题已修复。
此处提到的片段

app.Use(async (context, next) =>{
            await next();
            if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                await next();
            }
        }); 

Net 6 and angular spa application refresh 404 issue appears.Along with index html changes add below lines in program/startup.cs refreshing issue fixed .
snippet mentioned here

app.Use(async (context, next) =>{
            await next();
            if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                await next();
            }
        }); 
腹黑女流氓 2025-01-23 08:09:27

这对我来说是这样的:

// Configure endpoints
app.UseEndpoints(endpoints =>
{
    // your other endpoint mappings...
    endpoints.MapFallbackToFile("index.html");
});

This did it for me:

// Configure endpoints
app.UseEndpoints(endpoints =>
{
    // your other endpoint mappings...
    endpoints.MapFallbackToFile("index.html");
});

ま柒月 2025-01-23 08:09:27

您是否尝试添加

 <action type="Rewrite" url="./index.html" />

而不是

 <action type="Rewrite" url="/" />

Did you try adding

 <action type="Rewrite" url="./index.html" />

instead of

 <action type="Rewrite" url="/" />
层林尽染 2025-01-23 08:09:27

在 .NET 7 上,在 Program.cs 中,这已经通过这行代码进行了处理。

app.MapFallbackToFile("index.html");

但是,如果您的 index.html 位于 wwwroot 内的子文件夹内,则以下是配置方法。

var fileProvider = new PhysicalFileProvider(
    Path.Combine(app.Environment.WebRootPath, "MySubFolder"));
app.MapFallbackToFile("index.html", new StaticFileOptions
{
    FileProvider = fileProvider,
    RequestPath = ""
});

如果index.html位于wwwroot之外的文件夹(例如:ClientApp/dist)中,则应像这样创建fileProvider

var fileProvider = new PhysicalFileProvider(
    Path.Combine(app.Environment.ContentRootPath, "ClientApp", "dist", "index.html"));

On .NET 7, in Program.cs this is already handled with this line of code.

app.MapFallbackToFile("index.html");

But if your index.html is inside a subfolder within wwwroot, this is how to configure it.

var fileProvider = new PhysicalFileProvider(
    Path.Combine(app.Environment.WebRootPath, "MySubFolder"));
app.MapFallbackToFile("index.html", new StaticFileOptions
{
    FileProvider = fileProvider,
    RequestPath = ""
});

If the index.html is in a folder (e.g.: ClientApp/dist) outside wwwroot, fileProvider should be created like this.

var fileProvider = new PhysicalFileProvider(
    Path.Combine(app.Environment.ContentRootPath, "ClientApp", "dist", "index.html"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文