【问题标题】:How to configure ASP.NET Core 3.1 application to run ReactJS and MVC website side by side如何配置 ASP.NET Core 3.1 应用程序以并行运行 ReactJS 和 MVC 网站
【发布时间】:2020-05-20 01:23:44
【问题描述】:

我有一个使用CRA 创建的 ASP.NET 核心反应网站。 React 应用程序适用于只读的面向公众的页面。

例如,我还希望将一些管理页面创建为 EF 核心脚手架 Razor 页面;

如何配置路由以适应这两种情况?

启动配置如下;

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllersWithViews();

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });

            services.AddDbContext<MyDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
        }

        // 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
            {
                app.UseExceptionHandler("/Error");
                // 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.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "admin",
                    pattern: "admin/{controller}/{action=Index}/{id?}");
            });

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

                if (env.IsDevelopment())
                {
                    //spa.UseReactDevelopmentServer(npmScript: "start");
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
                }
            });
        }

【问题讨论】:

    标签: asp.net-mvc reactjs asp.net-core-3.1 asp.net-core-routing


    【解决方案1】:

    startup.cs 中,端点映射是为 MVC 配置的。这可以工作,但改变它会更容易;

    改变

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
        name: "admin",
        pattern: "admin/{controller}/{action=Index}/{id?}");
    });
    

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
    });
    

    https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/https://www.learnrazorpages.com/razor-pages/routing 获得的信息

    【讨论】:

    • @mausinc 这从未投入生产,只是为了证明概念
    • 啊,是的,我在生产中运行 mvc 路由和 spa 时遇到了一些问题。我得再看看,谢谢回复。
    • 我的理解是,这应该适用于您托管的任何环境。当我上传到 Azure 网站时它工作。您是否有机会使用 react-router?
    • 我正在使用 react-router 是的
    • 在 VS 2019 中使用 React 模板重现添加 Mvc 项目 (3.1)。添加 HomeController 和索引视图开发 /Home/Index 仅在 ctrl+f5 后按预期显示 MVC 视图生产 (office365demo.azurewebsites.net/home/index)
    猜你喜欢
    • 2015-03-29
    • 2022-01-02
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多