gpt4 book ai didi

c# - 在 Debian 8.10 上启动 ASP.NET Core React Web 应用程序时出错

转载 作者:行者123 更新时间:2023-11-30 18:11:39 24 4
gpt4 key购买 nike

我创建了 ASP.NET Core React Web 应用程序(VS 2017,Core 2.2,模板“React.js 和 Redux”)

唯一的代码修改是这样的:

public class Program
{
...
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
#if !DEBUG
.UseStartup<Startup>()
.UseUrls("http://*:80");
#else
.UseStartup<Startup>();
#endif
}

然后,我使用以下参数发布了它:

  • 发布方法“文件系统”;
  • 配置:发布;
  • 部署模式:自包含;
  • 目标运行时:linux-x64

之后,我将文件从“publish”文件夹复制到 Debian 服务器(Debian GNU/Linux 8.10 (jessie))

然后我开始了:

    root@server:~# dotnet "/var/aspnetcore/publish/WebApplicationReactRedux.dll"
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Content root path: /root
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

至此,一切都还比较顺利。但是当我尝试打开该站点时,出现以下错误:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.
Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

at Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware.<>c__DisplayClass0_0.<Attach>b__1(HttpContext context, Func`1 next)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'Page: /Error'
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[3]
Route matched with {page = "/Error", action = "", controller = ""}. Executing page /Error
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[101]
Executing handler method WebApplicationReactRedux.Pages.ErrorModel.OnGet - ModelState is Valid

我尝试用常规的 .NET Core MVC 网络应用程序做同样的事情,没有任何问题。

最佳答案

我花了一些白发,但看起来我想通了。由于某些原因,ClientApp 文件夹的相对路径解析错误。在下面的源代码中,我标记了放置绝对路径而不是相对路径的地方。至少,在那之后网站开始正常运行。

public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSpaStaticFiles(configuration =>
{
//configuration.RootPath = "ClientApp/build"; <-- this relative path resolves wrong
configuration.RootPath = "/var/aspnetcore/publish/ClientApp/build";
});
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseSpa(spa =>
{
//spa.Options.SourcePath = "ClientApp"; <-- this relative path resolves wrong
spa.Options.SourcePath = "/var/aspnetcore/publish/ClientApp";
...
});
}
}

但是我仍然无法解释为什么相对路径解析错误以及如何修复它。

关于c# - 在 Debian 8.10 上启动 ASP.NET Core React Web 应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57239856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com