c# - 将 API 控制器添加到 ASP.NET Core 项目
问题描述
这是搭建和编辑的控制器。
namespace TheAspNetCoreProject
{
[Route("api/TheApi")]
[ApiController]
public class TheApiController : ControllerBase
{
[HttpGet]
public IActionResult GetInfo()
{
return Ok("Foo");
}
}
}
我希望看到它/api/TheApi/
或/api/TheApi/Info
或/api/TheApi/GetInfo
但什么都没有。
脚手架没有做任何事情Startup.cs
,但是例如在文档中没有提到需要任何东西Startup.cs
——这听起来不太可能。
我怎样才能让它工作?
解决方案
I've just fired up VS and did a new .net core Web Api project from template and it works as you would expect to work:
[ApiController]
[Route("api/TheApi")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[HttpGet("OtherGet")]
public IEnumerable<WeatherForecast> OtherGet()
{
var rng = new Random();
return Enumerable.Range(1, 2).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
Valid urls:
- https://localhost:44308/api/TheApi -->calls the Get method
- https://localhost:44308/api/TheApi/Get -->calls the Get method
- https://localhost:44308/api/TheApi/OtherGet -->calls the OtherGet method, that have the OtherGet route name in the HttpGet annotation.
Startup.cs looks like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
UPDATE: Also keep in mind that you can not have multiple GET/POST etc methods in one controller, only if you use the annotation for the others (or for all of them) like I did with OtherGet: HttpGet("OtherGet")]. If you don't use this annotation and have multiple GET, POST etc methods you will have exception: AmbiguousMatchException: The request matched multiple endpoints.
推荐阅读
- android - 我可以上传多少个 apk 到 google play 和 admob
- android - 在后台杀死的活动
- javascript - 如何在 jszip forEach 中保存解压缩的内容
- git - macOS Big Sur - 无法安装 Brew
- scilab - scilab/xcos V6.1下如何绘制波特图?
- javascript - Tab.Navigator -> 如何使用组件?
- postgresql - 在没有 ID 的情况下通过 rand() 更快地订购
- javascript - 服务器重新加载后nodejs auth cookie过期
- javascript - 按日期分组并在 JSON 对象数组中查找温度的最小值和最大值
- netlogo - 停用节点的路径