gpt4 book ai didi

asp.net-core - 如何在 Visual Studio 2022 中使用 React(独立)部署 ASP.NET Core Web API 项目

转载 作者:行者123 更新时间:2023-12-02 18:03:46 26 4
gpt4 key购买 nike

我就关注https://github.com/MicrosoftDocs/visualstudio-docs/blob/main/docs/javascript/tutorial-asp-net-core-with-react.md将我的项目发布到本地文件夹,然后在 iis 上添加新网站。但我不知道如何浏览在 wwwroot 文件夹中创建的 index.html 以使用 api,浏览结果为:未找到该网址的网页:http://localhost:7080/wwwroot/index.htmlHTTP 错误 404

是否应该在 iis 上创建两个独立的站点?

程序.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();

app.Run();

enter image description here

索引文件:

enter image description here

浏览网站:

enter image description here

结果:

enter image description here

测试 api 的 postman 成功了:

enter image description here

最佳答案

我将program.cs更改为下面的内容,它成功了!

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.UseAuthorization();

//app.MapControllers();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();

// route non-api urls to index.html
endpoints.MapFallbackToFile("/index.html");
});

app.Run();

关于asp.net-core - 如何在 Visual Studio 2022 中使用 React(独立)部署 ASP.NET Core Web API 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73769824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com