I have a project that I created based on the ASP.NET Core 3 Angular template that works great locally. However, when I deploy the application to an Azure App Service, the site will start, but then immediately fails when the first API calls it makes against the server fails because it's returning the contents of index.html, rather than the expected API results. This particular call SHOULD be returning a simple true/false, but is index.html instead. Here's my Startup.cs:
using System;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StaticSphere.Bookings.Converters;
using StaticSphere.Bookings.Data;
using StaticSphere.Bookings.Extensions;
using StaticSphere.Bookings.Middleware;
namespace StaticSphere.Bookings
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
services.AddDbContext<BookingsDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddConfiguredIdentity(Configuration); // This just wraps up my ASP.NET Identity configuration
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
services.AddAutoMapper(typeof(Startup));
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
services.AddLocalRegistrations(); // This wraps up my custom services
}
public static 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.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseWhen(context =>
context.Request.Path.StartsWithSegments("/api", StringComparison.InvariantCultureIgnoreCase) &&
!context.Request.Path.StartsWithSegments("/api/auth", StringComparison.InvariantCultureIgnoreCase),
builder =>
{
builder.UseMiddleware<ActivityMiddleware>(); // This just makes a call to log user activity to the database.
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{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");
spa.Options.StartupTimeout = TimeSpan.FromSeconds(120);
}
});
}
}
}
I've briefly experimented with using rewriting, but didn't have any success with it, as none of the examples that I found online seemed set up to work with the app.UseSpa call, nor does it seem logical that I would need to use it, since the calls work correctly at DEV time, but maybe I missed something obvious?
Thanks in advance for any help here!
Startup.csby redirecting to/Errorbecause of this default line:app.UseExceptionHandler("/Error");Well, I didn't have a controller to handle this route so instead it returned the default pageindex.html. I resolved this by creating a web api controller named ErrorController and changing the line toapp.UseExceptionHandler("/api/Error/Error");.var exceptionHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();; Using this I grabbed and returned the error message.