C# angular应用程序在http://localhost:4200,但我想https://localhost:44325
我有一个ASP.NET Core 2.1 Web应用程序,它带有一个角度前端(使用VS默认设置),配置为通过HTTPS连接运行 最初我负责这个项目,而且效果很好。 但是,现在,在编辑了.html和.ts文件之后,更改不会继续进行。但当我在上运行服务器时,更改会继续进行 我希望更改能够继续进行,因为这是我运行应用程序时浏览器打开的端口。我如何改变这个?这是npm问题吗?它在我的angular应用程序中吗?它在Startup.cs中吗 Startup.csC# angular应用程序在http://localhost:4200,但我想https://localhost:44325,c#,angular,asp.net-core-2.1,C#,Angular,Asp.net Core 2.1,我有一个ASP.NET Core 2.1 Web应用程序,它带有一个角度前端(使用VS默认设置),配置为通过HTTPS连接运行 最初我负责这个项目,而且效果很好。 但是,现在,在编辑了.html和.ts文件之后,更改不会继续进行。但当我在上运行服务器时,更改会继续进行 我希望更改能够继续进行,因为这是我运行应用程序时浏览器打开的端口。我如何改变这个?这是npm问题吗?它在我的angular应用程序中吗?它在Startup.cs中吗 Startup.cs ... public void Confi
...
public void ConfigureDevelopmentServices(IServiceCollection services)
{
...
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Fallback", action = "Index" }
);
});
...
}
...
export const environment = {
production: false,
apiUrl: 'http://localhost:5000/api/'
};
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");
}
});
apiUrl: 'https://localhost:44325/api/
环境。ts
...
public void ConfigureDevelopmentServices(IServiceCollection services)
{
...
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Fallback", action = "Index" }
);
});
...
}
...
export const environment = {
production: false,
apiUrl: 'http://localhost:5000/api/'
};
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");
}
});
apiUrl: 'https://localhost:44325/api/
您可以通过编辑.angular-cli.json文件在所需端口上运行angular应用程序 只需将“服务器”部分添加到该文件的“默认值”部分。您可以用所需的端口替换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)
{
...
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Fallback", action = "Index" }
);
});
...
}
...
export const environment = {
production: false,
apiUrl: 'http://localhost:5000/api/'
};
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");
}
});
apiUrl: 'https://localhost:44325/api/
环境。ts
...
public void ConfigureDevelopmentServices(IServiceCollection services)
{
...
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Fallback", action = "Index" }
);
});
...
}
...
export const environment = {
production: false,
apiUrl: 'http://localhost:5000/api/'
};
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");
}
});
apiUrl: 'https://localhost:44325/api/
(在Environment.prod.ts中,这是“api/”)我尝试过,但出现了错误:
System.aggregateeexception:发生了一个或多个错误。(试图以其访问权限所禁止的方式访问套接字)
您是否可能尝试使用已在使用的端口?我已清除了缓存,在匿名中尝试过(清除缓存之前和之后),清除了npm缓存,并清除了VS的用户缓存。我已经用这个把头撞在墙上好一阵子了。。。