繁体   English   中英

带有角度模板的 asp.net core 3.1 无法再响应 404 并始终重定向到 spa index.html 页面

[英]asp.net core 3.1 with angular template can no longer respond with 404 and always redirect to spa index.html page

我有一个带有剃刀页面的 asp.net core 3.1 项目。 我添加了一个带有<base href="/app" />角度的水疗中心,以便该应用程序在/app下作为服务器。 现在,任何不存在的路由都会返回 spa index.html 页面而不是 404。

我的 angular 应用程序上没有任何客户端路由。

如果没有与剃刀页面、api 和/app都匹配的路由,我想要的行为是使用 404 进行响应。

但是,当我输入localhost:5000/non-existent-route ,应用程序重定向到我的 angular index.html 应用程序,这不是所需的行为。

基本上,当没有路由匹配时,我想防止 angular 应用程序的index.html成为后备默认值。

这是我的 Startup.cs 文件

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });
        services.AddSwaggerDocument();
        services.AddHealthChecks();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });
        // ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        // app.UseHttpsRedirection();
        app.UseStaticFiles();
        if (!env.IsDevelopment())
        {
            app.UseSpaStaticFiles();
        }

        app.UseOpenApi();
        app.UseSwaggerUi3();

        app.UseRouting();

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

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

不确定您是否可以在服务器端执行此操作,但可以在 Angular 端执行此操作,如下所示:

@NgModule({
  imports: [
    NgbModule.forRoot(),
    RouterModule.forRoot([
      {
        path: '', component: MainParentComponent, children: [
            { path: '**', component: ErrorComponent } //Default redirection for invalid URLs or unauthorised URLs
        ]
      }
    ])
  ]
})

暂无
暂无

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

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