ASP.NET Core 3 Angular API调用返回index.html

问题描述 投票:0回答:1

我有一个基于ASP.NET Core 3 Angular模板创建的项目,该模板在本地效果很好。但是,当我将应用程序部署到Azure App Service时,该站点将启动,但是当第一个对服务器进行的API调用失败时,该站点将立即失败,因为它返回的是index.html的内容,而不是预期的API结果。这个特殊的调用应该返回一个简单的true / false,但是应该是index.html。这是我的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);
                }
            });   
        }
    }
}

我已经简要地尝试过使用重写,但是没有成功,因为我在网上发现的所有示例似乎都没有设置为可以与app.UseSpa调用一起使用,我似乎也没有逻辑。需要使用它,因为调用在DEV时可以正常工作,但是也许我错过了一些明显的事情?

在此先感谢您的帮助!

angular api asp.net-core routing
1个回答
0
投票

感谢Zach对这个问题的见解!最终,我发现该数据库不可访问,需要打开对我的应用程序的访问。我对Azure来说还很陌生,所以我仍在继续学习。

© www.soinside.com 2019 - 2024. All rights reserved.