映射SPA asp.net核心外部的特定路由

时间:2020-07-21 21:09:44

标签: c# asp.net 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。 SPA仍然可以按预期运行,但是simulate路由似乎被忽略了。任何帮助将不胜感激。

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

        }

1 个答案:

答案 0 :(得分:1)

您是否尝试过向控制器添加<template> <div> <label v-for="strategy in groups" :key="strategy"> <input :name="'strategy' + groupId" type="radio"> {{strategy}} </label> </div> </template> <script> let groupId = 0 export default { props: { groups: Array }, data() { return { groupId: groupId++ } } } </script> 属性?

Route

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

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

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