【问题标题】:Error on starting ASP.NET Core React web applicaton on Debian 8.10在 Debian 8.10 上启动 ASP.NET Core React Web 应用程序时出错
【发布时间】:2019-07-28 10:08:55
【问题描述】:

我创建了 ASP.NET Core React Web Applicaton(VS 2017,Core 2.2,模板“React.js and 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 应用程序做同样的事情,没有任何问题。

【问题讨论】:

    标签: c# reactjs visual-studio asp.net-core debian


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      相关资源
      最近更新 更多