ASP.NET Core: The SPA default page middleware could not return the default page '/index.html' because it was not found

Viewed 554

I am trying to deploy my Angular and .Net Core app using IIS but I get the following error message

The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

This is what my startup.cs looks like

namespace GammaFramework { 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        services.AddMvc(option => option.EnableEndpointRouting = false);

        //services.AddDbContext<GammaFrameworkContext>(options =>
        //  options.UseSqlServer(Configuration.GetConnectionString("GammaFrameworkContext")));

        services.AddDbContext<GammaFrameworkContext>();

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        });

        services.AddScoped(typeof(IDataRepository<>), typeof(DataRepository<>));

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });

        services.AddControllersWithViews(options =>
        {
            options.AllowEmptyInputInBodyModelBinding = true;
        });
    }

  // This method gets called by the runtime. Use this method to

configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // 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.UseCors("CorsPolicy");
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

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

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

inside my wwwroot folder I have: api folder: webapi project app folder: build angular folder web.config: copied from webapi project

0 Answers
Related