c# - 在 Debian 8.10 上启动 ASP.NET Core React Web 应用程序时出错
问题描述
我创建了 ASP.NET Core React Web Applicaton(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 Web 应用程序做同样的事情,没有任何问题。
解决方案
它花了我一些白发,但看起来我想通了。由于某些原因,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";
...
});
}
}
但是我仍然无法解释为什么相对路径解析不正确以及如何修复它。
推荐阅读
- javascript - 如果没有 ios 中的特定按钮,则无法通过触摸关闭弹出窗口
- perforce - 在 P4 客户端上设置默认描述模板
- powershell - 如何在 Where-Object Powershell 中使用 If Else
- c# - 图像颜色在 Unity 中没有变化
- mediawiki - 模板打印空段落
- python - 如何在复杂的字典列表中获取键的完整路径
- mysql - 错误 1045 <28000>:拒绝用户访问
- c# - c# asp .net core HTTP POST Request 适用于 Postman 但不适用于我的 Angular 客户端(404 错误)
- c# - 在 C# 中获取我的 JSON 数据的特定部分
- angular - Ionic 3,限制离子输入的十进制字符数