【问题标题】:netcore mvc presedence over spa caches angular build and breaks hmrnet core mvc 优先于 spa 缓存 angular build 并破坏 hmr
【发布时间】:2018-12-20 04:42:24
【问题描述】:

我有一个 dotnet 2.1 Web 应用程序,MVC 运行良好。我正在升级它,以使用 SPA 框架。我的目标是让两个路由同时使用。选择 Angular 6,到目前为止一切顺利。我用 MVC 引擎渲染我的剃须刀页面,然后,如果路由 url 不匹配任何服务器控制器,它将转到客户端应用程序,这没关系。

在我的创业课程中,我有:

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

  app.UseSpa(spa =>
  {
    spa.Options.SourcePath = "wwwroot/dist";
    if (env.IsDevelopment())
      spa.UseProxyToSpaDevelopmentServer("http://localhost:4200/");
  });

问题是,当我有第一个MVC,然后在configure方法中进行SPA,热模块替换被忽略,甚至刷新页面都没有检索到新鲜内容,就像如果MVC先行,它会缓存角度构建,直到您重建客户端应用程序[在所有文件上使用哈希(ng build --output-hashing=all)],然后重新启动调试,您将看不到更改。一种解决方法是在每次发生更改时以隐身模式打开浏览器。

如果我评论“app.UseMvc()”或将其放在“app.UseSpa()”之后,热模块替换会正确完成工作并更新页面重新加载。有什么我可能会丢失的吗?

如果有什么办法可以先进行 SPA 路由,如果是 404,然后从 Angular 中的应用路由模块重定向到服务器端路由?

谢谢

【问题讨论】:

    标签: angular model-view-controller routing .net-core single-page-application


    【解决方案1】:

    问题是我使用的是 SwaggerUI,而我在一切之前都在调用它。 为了路由 MVC,Swagger UI 和 SPA 的 index.html,它应该按以下顺序:

    app.UseStaticFiles();
    
    app.UseMvc(routes =>
    {
      routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}");
    });
    
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
      c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
      c.RoutePrefix = "api";
    });
    
    app.UseSpa(spa =>
    {
      spa.Options.SourcePath = "wwwroot/dist";
      if (env.IsDevelopment())
        spa.UseProxyToSpaDevelopmentServer("http://localhost:4200/");
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2018-10-29
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2022-12-30
      • 2018-03-27
      相关资源
      最近更新 更多