首页 > 解决方案 > 在 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
    }

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

之后,我将文件从文件夹“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 应用程序做同样的事情,没有任何问题。

标签: c#reactjsvisual-studioasp.net-coredebian

解决方案


它花了我一些白发,但看起来我想通了。由于某些原因,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"; 
            ...
        });
    }
}

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


推荐阅读