在相同的解决方案问题中使用create-react-app前端创建.NET Core 3.0 WebAPI时出错

时间:2019-06-30 20:05:20

标签: asp.net-core-webapi create-react-app .net-core-3.0

我尝试了几种不同的方法来设置此项目,但每种方法均失败。这是我的问题。

  • 使用命令提示符按照此article中的指示创建这些项目有一个主要缺陷,如果生成这些缺陷,然后在VS 2019中将其打开,则可以立即使用它,没有问题。添加文件的第二秒钟,它会提示您保存解决方案文件,此后,整个解决方案将崩溃​​,您将无法再启动它。...所以此选项似乎很麻烦。
  • 我试图用VS 2019创建一个.net core 3应用程序,使其成为WebAPI应用程序。之后,我进入根目录,并在命令提示符下输入: npx create-react-app client ,它似乎可以正确创建项目。之后,我进入了startup.cs文件,添加了该文件运行所需的spa项目(添加了NuGet包Microsoft.AspNetCore.SpaServices.Extensions)

这是我的startup.cs代码:

   public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddNewtonsoftJson();

        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "client/build";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseRouting(routes =>
        {
            routes.MapControllers();
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "client";
            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });

        app.UseAuthorization();
    }
}

运行此命令时, spa.UserReactDevelopmentServer(npmScript:“ start”)所在的行会不断抛出错误。错误是:

{“找不到方法:'无效Microsoft.Extensions.Logging.Console.ConsoleLogger..ctor(System.String,System.Func`3,Boolean)'。“}

有没有人知道如何使用React前端(不是类型脚本版本)创建一个.NET Core 3.0 WebAPI,当您点击播放时(VS 2019中的绿色继续按钮),它们都将运行React,并且仍然API部分在调用时可以正常工作。对于清酒而言,这应该不难!

1 个答案:

答案 0 :(得分:2)

我有同样的问题。

将项目.csproj中的“ Microsoft.AspNetCore.SpaServices.Extensions”的参考更改版本更改为“ 3.0.0-preview8.19405.4”对我有用。