简体   繁体   中英

Map specific route outside SPA asp.net core

I'm working on an SPA asp.net core-3 app. I'm trying to have two routes:

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

I feel I had it covered using the MapWhen in the Startup configuration, however when i browse to localhost:5000/simulate I get localhost didn't send any data. ERR_EMPTY_RESPONSE localhost didn't send any data. ERR_EMPTY_RESPONSE . The SPA still works as expected, but the simulate route seems to be ignored. Any help would be appreciated.

SimulateController.cs

namespace Charla.Controllers {
    
    public class SimulateController : ControllerBase {

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

    }
    
}

Startup.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");
                        }
                    });
            });

        }

Have you tried adding a Route attribute to your Controller?

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

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

And Add MapController to endpoints instead of 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");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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