繁体   English   中英

Map特定路由外SPA asp.net芯

[英]Map specific route outside SPA asp.net core

我正在开发一个 SPA asp.net core-3 应用程序。 我试图有两条路线:

/simulate -> This routes outside SPA, to a basic controller that produces raw html
/*        -> any other routes will route to the angular SPA

我觉得我在启动配置中使用MapWhen覆盖了它,但是当我浏览到localhost:5000/simulate时,我得到localhost didn't send any data. ERR_EMPTY_RESPONSE localhost didn't send any data. ERR_EMPTY_RESPONSE SPA 仍然按预期工作,但simulate路线似乎被忽略了。 任何帮助,将不胜感激。

模拟控制器.cs

namespace Charla.Controllers {
    
    public class SimulateController : ControllerBase {

        [HttpGet]
        public ContentResult Index() 
        {
            return base.Content("<html><body><div>Hello</div></body></html>", "text/html");
        }

    }
    
}

启动.cs


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
        {
            
...
            app.UseRouting();

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


            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<ChatHub>("/hub");
            });

            app.MapWhen(
                ctx => !ctx.Request.Path.StartsWithSegments("/simulate"), 
                appBuilder => { 
                    app.UseSpa(spa => {
                        // To learn more about options for serving an Angular SPA from ASP.NET Core,
                        // see https://go.microsoft.com/fwlink/?linkid=864501

                        spa.Options.SourcePath = "ClientApp";

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

        }

您是否尝试向 Controller 添加Route属性?

[Route("[controller]")]
public class SimulateController : ControllerBase {

    [HttpGet]
    public ContentResult Index() 
    {
        return base.Content("<html><body><div>Hello</div></body></html>", "text/html");
    }
}

并将 MapController 添加到端点而不是 UseMvc?

app.UseRouting();

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

    endpoints.MapHub<ChatHub>("/hub");
});

app.UseSpa(spa =>
{
    // To learn more about options for serving an Angular SPA from ASP.NET Core,
    // see https://go.microsoft.com/fwlink/?linkid=864501

    spa.Options.SourcePath = "ClientApp";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM