【问题标题】:Can't get React app to run in Visual Studio无法让 React 应用程序在 Visual Studio 中运行
【发布时间】:2019-12-22 09:43:50
【问题描述】:

我有一个 React/Express 应用程序,我正试图将它制作成一个 React/WebAPI 应用程序。我无法让反应应用程序运行。如果我点击 IIS Express 按钮,就会出现 Views/Home 文件夹中的索引文件。如果我在命令提示符下键入npm start,Chrome 会说它正在加载localhost:3000 一段时间,然后什么都没有弹出。我将包含一些代码和我的文件夹设置。我是 C# 和 Visual Studio 的新手,所以我迷路了。我希望有人可以提供帮助。

来自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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the React files will be served from this directory
        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, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
            app.UseSpaStaticFiles();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }
}

程序.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

我的文件夹结构的图片。 'client' 是我的 react 应用程序所在的位置

【问题讨论】:

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


    【解决方案1】:

    当我创建一个新的 React asp.net 核心模板时,它在使用 IIS express 和在 client 文件夹上使用 npm start 时都能正常工作。看来你使用了错误的路径,尝试修改下面的代码

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

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

    【讨论】:

    • 谢谢。我还必须删除 app.UseMvc 才能正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多