简体   繁体   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

I have an asp.net core 3.1 project with razor pages.我有一个带有剃刀页面的 asp.net core 3.1 项目。 I added a spa with angular with <base href="/app" /> so that the app is server under /app .我添加了一个带有<base href="/app" />角度的水疗中心,以便该应用程序在/app下作为服务器。 Now, any non existent route returns the spa index.html page instead of 404.现在,任何不存在的路由都会返回 spa index.html 页面而不是 404。

I don't have any client routing on my angular app.我的 angular 应用程序上没有任何客户端路由。

My desired behavior is to respond with 404 if there is no route matching neither razor pages, the api, nor the /app .如果没有与剃刀页面、api 和/app都匹配的路由,我想要的行为是使用 404 进行响应。

However, when i type localhost:5000/non-existent-route , app redirects to my angular index.html app which is not the desired behavior.但是,当我输入localhost:5000/non-existent-route ,应用程序重定向到我的 angular index.html 应用程序,这不是所需的行为。

Basically, i want to prevent the angular app's index.html from being the fallback default when no route is matched.基本上,当没有路由匹配时,我想防止 angular 应用程序的index.html成为后备默认值。

Here is my Startup.cs file这是我的 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");
            }
        });
    }
}

Not sure if you can do this on the server side, but it is possible on Angular side as below:不确定您是否可以在服务器端执行此操作,但可以在 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.

相关问题 带有 React 模板的 ASP.NET Core 返回 index.html - ASP.NET Core with React template returns index.html 在 asp.net core 中将 index.html 设置为默认页面 - Setting index.html as default page in asp.net core 迁移到 ASP.NET 3.1 - 从登录控制器路由到 Razor 页面主页索引页面不再有效 - Migration to ASP.NET 3.1 - routing from Login Controller to Razor Page Home Index page no longer working ASP.NET Core 3.1 区域在某些方法上返回 404,但在索引一上不返回 - ASP.NET Core 3.1 areas return 404 on some methods but not on the Index one ASP.NET Core 3.1 中的默认页面 - Default page in ASP.NET Core 3.1 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC 具有Windows身份验证的Asp.Net Core 2.1 SPA React模板 - Asp.Net Core 2.1 SPA React Template with Windows Authentication Asp.net core 3.1 with Razor 页面重定向到索引页面而不是预期页面 - Asp.net core 3.1 with Razor Pages redirects to the Index page instead of the intended page 配置 ASP.Net Core Antiforgery 以使用 Angular SPA - Configure ASP.Net Core Antiforgery to work with Angular SPA 如何在本地部署 Asp.Net Core 6 + Angular SPA - How deploy Asp.Net Core 6 + Angular SPA locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM