【发布时间】:2020-11-11 15:30:40
【问题描述】:
我使用以下命令在 Visual Studio Code 中创建了一个项目:
dotnet new angular --use-local-db
此后,我将 Angular 从版本 8 更新到 10 并完成了项目。我测试使用
dotnet watch run
对我来说一切正常,我尝试在本地 IIS 上发布它。可悲的是,我不会让它运行。我尝试了在几个页面上找到的不同解决方案,最终使用 Visual Studio 在 IIS 上发布项目。我使用了以下说明,除了 Code First 迁移,因为必要的数据库已经存在: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-to-iis#create-the-publish-profile
问题是,如果我这样做,Visual Studio 将创建一个名为“myproject”的“应用程序”。然后当我在其上运行“浏览...”时,我将在控制台中收到以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found) style. ... .css
Failed to load resource: the server responded with a status of 404 (Not Found) runtime-es2015. ... .css
Failed to load resource: the server responded with a status of 404 (Not Found) polyfills-es2015. ... .css
Failed to load resource: the server responded with a status of 404 (Not Found) main-es2015. ... .css
提到的文件夹在提到的目录('ClientApp\dist')上。有什么我错过的配置或设置吗?
任何帮助将不胜感激。
第一次回复后编辑:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseHttpsRedirection();
app.UseStaticFiles();
...
app.UseRouting();
...
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
第二次回复后编辑:
这里有一些目录/路径。希望在这里找到需要的一次。
项目结构
[Root]
ClientApp
src
app
[Angular Web App related files]
app.module.ts
...
main.js
index.html
styles.css
polyfills.ts
...
package.json
angular.json
[ASP.NET Core Web API Files]
appsettings.json
[Project].csproj
Program.cs
Startup.cs
IIS 文件
wwwroot
[Project]
ClientApp
dist
index.html
main. ... .js
polyfills. ... .js
runtime. ... .js
styles. ... .css
[ASP.NET Core Web API Files]
如何在浏览器中调用项目:
localhost/[Project]
当我按下 ctrl+U 并尝试访问文件时显示的路径:
Requested URL: localhost:Port/main. ... .js
Physical Path: ...\wwwroot\main. ... .js
新观察:
如果我从
修改角度客户端的 index.html <base href="/" />
到
<base href="/[projectname]/" />
页面正在 IIS 上的 Prod 中加载,但在开发模式下失败。 然后在 IIS 上的 Prod 中,它使用 url localhost/api/[controller] 而不是工作 url localhost/[projectname]/api/[controller]。最后提到的 url 我可以通过在浏览器中输入它来访问它并返回数据。
如果我随后修改控制器的服务文件来自
...
return this.http.get('/api/[controller]')
...
到
...
return this.http.get('[projectname]/api/[controller]')
...
它尝试访问路径:localhost/[projectname]/[projectname]/api/[controller]
知道我配置错误或需要更改配置以使项目在 Prod 和 Dev 模式下工作吗?
任何帮助将不胜感激。
【问题讨论】:
标签: c# asp.net angular iis asp.net-core-3.1