【问题标题】:angular app works on http://localhost:4200, but I want https://localhost:44325角度应用程序适用于 http://localhost:4200,但我想要 https://localhost:44325
【发布时间】:2018-12-01 06:35:54
【问题描述】:

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

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

我希望在https://localhost:44325 上进行更改,因为这是我运行应用程序时浏览器打开的端口。我该如何改变?是 npm 的问题吗?它在我的角度应用程序中吗?它在 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/'
};

【问题讨论】:

    标签: c# angular asp.net-core-2.1


    【解决方案1】:

    您可以通过编辑 .angular-cli.json 文件在所需端口上运行您的 Angular 应用程序 只需将“服务器”部分添加到该文件中的“默认值”部分。您可以将 2500 替换为您想要的端口。

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

    【讨论】:

    • 我试过了,但我得到了错误:System.AggregateException: One or more errors occurred. (An attempt was made to access a socket in a way forbidden by its access permissions)
    • 您是否有可能尝试使用已在使用的端口?
    【解决方案2】:

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

    <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>
    

    【讨论】:

    • 我已经清除了我的缓存,在隐身模式下尝试(在清除缓存之前和之后),清除了我的 npm 缓存,并清除了 VS 的用户缓存,只是为了更好地衡量。我已经用这个把头撞在墙上一段时间了......
    【解决方案3】:

    在与 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/')

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 2019-09-10
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2020-12-01
      • 1970-01-01
      • 2018-11-15
      相关资源
      最近更新 更多