angular 应用程序在 http://localhost:4200 上运行,但我想要 https://localhost:44325

angular app works on http://localhost:4200, but I want https://localhost:44325

我有一个 ASP.NET Core 2.1 Web 应用程序设置了一个 Angular 前端(使用 VS 默认设置),该应用程序配置为 运行 通过 HTTPS 连接。

我最初 运行 这个项目,它在 https://localhost:44325 上运行良好。 但是,现在,在编辑我的 .html 和 .ts 文件后,更改不会继续。但是当我 运行 http://localhost:4200 上的服务器时,更改会继续。

我希望更改在 https://localhost:44325 上继续进行,因为这是我 运行 应用程序时浏览器打开的端口。我该如何改变这个?这是一个 npm 问题吗?它在我的 angular 应用程序中吗?它在 Startup.cs 中吗?

Startup.cs

...
public void ConfigureDevelopmentServices(IServiceCollection services)
{
    ...
    app.UseMvc(routes =>
    {
        routes.MapSpaFallbackRoute(
            name: "spa-fallback",
            defaults: new { controller = "Fallback", action = "Index" }
        );
    });
    ...
}
...



environment.ts

export const environment = {
  production: false,
  apiUrl: 'http://localhost:5000/api/'
};

您可以通过编辑 .angular-cli.json 文件 运行 在所需端口上 angular 应用程序 只需将 "server" 部分添加到该文件的 "defaults" 部分。您可以将 2500 替换为您想要的端口。

"defaults": {
    "serve": {
        "port": 2500
    }
}

我敢打赌,问题是您的浏览器缓存了您最初在端口 44325 上使用的文件。您可以尝试在浏览器中手动清除 url 的缓存。我将此添加到 ASP.NET 项目的 web.config 文件中,以帮助解决类似问题。不确定 ASP.NET 核心是否有类似的机制。

<system.webServer>
        ...
        <httpProtocol>
            <!-- TODO Remove for production -->
            <customHeaders>
                <!-- Disable Cache  -->
                <add name="Cache-Control" value="no-cache, no-store, must-revalidate"/>
                <add name="Pragma" value="no-cache"/>
                <add name="Expires" value="0"/>
            </customHeaders>
        </httpProtocol>
    </system.webServer>

与 Angular/Core 2 应用程序的默认配置进行比较后,我发现了不同之处。

Startup.cs

    public void ConfigureDevelopmentServices(IServiceCollection services)
    {
        ...
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
        ...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
        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.UseProxyToSpaDevelopmentServer("http://localhost:4200");
                //spa.UseAngularCliServer(npmScript: "start");
            }
        });



Environment.ts

apiUrl: 'https://localhost:44325/api/

(在Environment.prod.ts,这是'api/')