c# - 如何在.net核心中调用带有参数的动作?
问题描述
所以我在 Startup.cs 中设置了以下默认模式:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
});
这是我的控制器:
[ApiController]
[Route("[controller]")]
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]
[Route("[action]")]
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, 0),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[HttpGet]
[Route("[action]")]
public IEnumerable<WeatherForecast> Grab()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(1, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[Route("[action]")]
[HttpGet("{i:int}")]
public IEnumerable<WeatherForecast> Snatch(int i)
{
var rng = new Random();
return Enumerable.Range(i, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(55, 100),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
现在,当我打电话时https://localhost:44388/weatherforecast/grab
,https://localhost:44388/weatherforecast/get
它工作得很好,但如果我打电话https://localhost:44388/weatherforecast/Snatch/1
,我就会Error: Cannot match any routes.
进入我的浏览器。
我已经用它期望一个int的参数设置了最后一个方法Snatch,那么我做错了什么?
解决方案
您需要使用一个路由模板来获得所需的行为
//GET weatherforecast/Snatch/1
[Route("[action]/{i:int}")]
[HttpGet]
public IEnumerable<WeatherForecast> Snatch(int i) {
//...
}
现在因为这是一个ApiController
,它只Http{Verb}
用于所有的动作就足够了
在构建 REST API 时,您很少需要
[Route(...)]
在操作方法上使用,因为该操作将接受所有 HTTP 方法。最好使用更具体Http*Verb*Attributes
的来准确了解您的 API 支持什么。REST API 的客户端应该知道哪些路径和 HTTP 动词映射到特定的逻辑操作。
//GET weatherforecast/Snatch/1
[HttpGet("[action]/{i:int}")]
public IEnumerable<WeatherForecast> Snatch(int i) {
//...
}
这可以通过聚合模板进一步简化
[ApiController]
[Route("[controller]/[action]")] //<-- NOTE THIS
public class WeatherForecastController : ControllerBase {
//...
//GET weatherforecast/get
[HttpGet]
public IEnumerable<WeatherForecast> Get() {
//...
}
//GET weatherforecast/grab
[HttpGet]
public IEnumerable<WeatherForecast> Grab() {
//...
}
//GET weatherforecast/Snatch/1
[HttpGet("{i:int}")]
public IEnumerable<WeatherForecast> Snatch(int i) {
//...
}
}
ASP.NET Core 中的参考路由
推荐阅读
- php - PHP ftp_connect 超时参数不起作用
- android - 如何在 Android 中将启动图像和文本居中在其下方?
- javascript - 重命名谷歌驱动器中的文件名
- c++ - c++ 编译器是否保护 const 内存地址不受任何更改?
- python - 在pycharm中导入模块
- npm - WP Rig:运行 npm run build 时出错:找不到“/vendor/bin/phpcs”
- draftjs - Draft.js:点击占位符开始输入
- python - 日期格式以及如何打印出现的最大数字和日期
- python - 我不能运行 seaborn
- c++ - 如何使用qt在给定时间在新窗口中显示视频帧