У меня есть ASP.NET-сервер, который обслуживает веб-сайт React, а также работает как API для самого веб-сайта. Сервер работает на ПК с Windows 11 и IIS в C:/MyWebSite. Эта папка содержит сервер ASP.NET (.exe, .dll и т. д.), конфигурацию IIS (web.config) и веб-сайт сборки React (index.html, favicon.ico и папку ресурсов).
Серверу удалось отобразить мою главную страницу, но ему не удалось выполнить запрос API. Запрос API также завершается неудачей, когда я вызываю его из Postman, и выдает ошибку «HTTP 404.0 — не найден» со следующими подробностями:
- Модуль IIS Web Ядро
- Уведомление: MapRequestHandler
- Обработчик: StaticFile
- Код ошибки: 0x80070002
Что касается ASP.NET, вот мой Program.cs:
Код: Выделить всё
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
// Create the web application builder
var builder = WebApplication.CreateBuilder(args);
// JWT authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
string? tKey = builder.Configuration["Jwt:Key"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = tKey != null ? new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tKey)) : null
};
});
// Add the controllers to the application (for input http requests)
builder.Services.AddControllers();
// Configure CORS policy
builder.Services.AddCors(options => {
options.AddPolicy("AllowAllOrigins",
builder => {
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Create the App
var app = builder.Build();
// Applies the CORS policy
app.UseCors("AllowAllOrigins");
// Serving the static files
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
// Map the routes to the controllers
app.MapControllers();
// Undefined route will lead to index.html
app.MapFallbackToFile("index.html");
// Run the App
app.Run();
Код: Выделить всё
using Microsoft.AspNetCore.Mvc;
namespace AspReact.Server.Controllers
{
[ApiController]
[Route("api/configuration")]
public class GeneralController : ControllerBase
{
[HttpGet("settings")]
public ActionResult GetSettings()
{
return Ok(new
{
language = 'fr',
theme = 0
});
}
[HttpPost("settings")]
public ActionResult SetSettings([FromQuery] string language, [FromQuery] string theme)
{
m_tLanguage = language;
m_tTheme = theme;
return Ok();
}
}
}
Код: Выделить всё
Код: Выделить всё
Обратите внимание, что все это работает во время разработка с сервером, работающим в консоли отладки.
Буду благодарен за любую помощь! Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/792 ... ot-working
Мобильная версия