这里有点不知所措。编写一个 .net Web 应用程序作为前端的后端,并且似乎无法让我的 api 路由注册/工作。当我尝试到达端点时,它几乎就像没有被视为控制器路由一样。该项目的目标是.net 7
程序.cs:
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using AppServices.Authentication;
using Microsoft.AspNetCore.DataProtection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDataProtection();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.Map("", client =>
{
var clientPath = Path.Combine(builder.Environment.ContentRootPath, "Client/dist");
StaticFileOptions clientAppDist = new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(clientPath)
};
client.UseSpaStaticFiles(clientAppDist);
client.UseSpa(spa =>
{
spa.Options.DefaultPageStaticFileOptions = clientAppDist;
});
});
app.UseAuth();
app.MapControllers();
app.Run();
有问题的控制器:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BFF.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
[HttpGet(Name = "test")]
public string GetTest()
{
return "test12345";
}
}
}
问题从这里开始:
app.Map("", client =>{ }
而不是整个地图块,只需执行以下操作:
app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Client/dist")),RequestPath = "/StaticFiles"});