我从 dotnet 开始,我立即遇到了这个问题。我做了一个简单的控制器,它应该返回一个字符串。然而,每次它都会抛出 404。此时我已经重写了这个 idk 多少次,结果仍然相同。似乎 dotnet 甚至没有将我的班级识别或注册为控制器。
这里是默认 VS 项目提供的示例控制器,这个像黄油一样工作:
using Microsoft.AspNetCore.Mvc;
namespace RPG.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
这是我的控制器,它不起作用:
using Microsoft.AspNetCore.Mvc;
namespace RPG.Controllers
{
[ApiController]
[Route("dungeon")]
public class DungeonController : ControllerBase {
[HttpGet]
public IEnumerable<string> Get() {
string[] a = { "spider" };
return a;
}
}
}
这是我的
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
if (!app.Environment.IsDevelopment()) {
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapControllers();
app.Run();
同样的事情在我使用 dotnet CLI 时起作用。有什么区别??
前端的代理配置设置不正确。