【问题标题】:.Net Core 2 & AngularJS SPA Not Working.Net Core 2 和 AngularJS SPA 不工作
【发布时间】:2018-08-09 20:55:50
【问题描述】:

我一直在将一个项目从 .NET Framework 迁移到 .NET Core 2,并且过程比预期的要顺利,但是当我认为我已经克服了最困难的挑战时,我终生无法获得SPA 方面的工作。

当我启动应用程序时,只要我转到根目录 (https://localhost:5001) 并且我可以很好地使用 Angular 路由进行导航,它就可以正常工作。但是,如果我尝试通过路由加载页面,例如。 https://localhost:5001/login,我得到一个通用的 Chrome 404“找不到这个本地主机页面”。

我的index.html 在目录MyApp/Pages/ 中,我的客户端代码在MyApp/wwwroot/app/ 中。将index.html 移动到wwwroot 文件夹时,我什么也得不到。

在我的Startup.cs 中,我是这样设置的:

public class Startup
{
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        } 

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        var defaultFileOptions = new DefaultFilesOptions();
        defaultFileOptions.DefaultFileNames.Clear();
        defaultFileOptions.DefaultFileNames.Add("/index.html");
        app.UseDefaultFiles(defaultFileOptions);
        app.Use(async (context, next) =>
        {
            await next();
            if (context.Response.StatusCode == 404 &&
                !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                context.Response.StatusCode = 200;
                await next();
            }
        });

        app.UseCookiePolicy();
        app.UseAuthentication();

        app.UseMvc();
    }
}

我尝试了许多指南,但似乎我能找到的大多数指南都非常过时,或者在尝试使用帖子中的方法时,它们是我没有的方法。非常感谢任何帮助,因为我很想完成迁移并继续从事该项目。

【问题讨论】:

    标签: c# angularjs asp.net-core asp.net-core-2.1 asp.net-core-mvc-2.0


    【解决方案1】:

    这里的问题是你有一个 SPA,而 Angular 有它自己的路由,但是当你尝试使用像 https://localhost:5001/login 这样的 URL 时,这个路由是由 MVC 而不是由 Angular 提供的,MVC 找不到合适的控制器并返回 404 作为结果。

    您应该做的是返回 index.html 以响应此类请求,以便 Angular 路由可以处理它。你可以看看这篇文章:

    https://www.toptal.com/angular/angular-5-asp-net-core

    【讨论】:

    • 感谢您的回复,该链接对我没有太大帮助,因为它是“新 Angular”,并且 Web.config 技巧对我不起作用。 “你应该做的是返回 index.html 以响应此类请求”是让我再次思考我的 startup.cs 代码的原因 - 我对 404 响应的处理是错误的!即将发布修复。
    【解决方案2】:

    事实证明我的问题在于我处理 404 的方式。我在 Startup.cs 中执行context.Request.Path = "/index.html";,但它仍然无法解析 html 文件,所以我将其重定向回根路径:

        app.Use(async (context, next) =>
        {
            await next();
            if (context.Response.StatusCode == 404 &&
                !Path.HasExtension(context.Request.Path.Value))
            {
                // This is what I changed. I need to resolve to "/" instead of "/index.html"
                context.Request.Path = "/";
                context.Response.StatusCode = 200;
                await next();
            }
        });
    

    感谢@Anton Danylov 的建议,让我重新审视那段代码。

    【讨论】:

      【解决方案3】:

      从此代码中使用:

      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          app.UseBrowserLink();
      }
      else
      {
          app.UseExceptionHandler("/Error");
      }
      
      app.UseStaticFiles();
      
      app.UseMvc(routes =>
      {
          routes.MapRoute(
              name: "default",
              template: "{controller}/{action=Index}/{id?}");
      });
      }
      

      关注链接:Application startup in ASP.NET Core

      【讨论】:

      • 这对我没有帮助,因为我实际上并不想要任何 MVC 路由。我发现了我的问题,即将发布。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2015-10-22
      • 2018-02-16
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多