我已经使用C#/ ASP.Net作了第一个终点,现在正尝试使用get请求来过滤“ yearId”。代码如下:
[HttpGet("{yearId}")]
public async Task<ActionResult<Student>> GetStudent(long yearId)
{
var student = await _context.Student.FindAsync(yearId);
if (student == null)
{
return NotFound();
}
return student;
}
我可以确认此帖子的输入正确无误,因此可以看到ID。如果我只尝试“ Id”,它将按唯一ID进行过滤。我的Startup.cs端点是默认端点,因此我怀疑这是问题所在,但不知道要去哪里:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
感谢您的帮助。我有以下路线:[Route(“ api / students”)]
谢谢
编辑:当我尝试/ api / students / getyear / 4时,我现在在邮递员中得到一个空白响应,在chrome中得到404。 Startup.cs是:
endpoints.MapControllerRoute (
name: "MapByAction",
pattern: "api/{controller}/{action}/{yearId?}",
defaults: new { controller = "students", action = `"getyear" });`
我得到的是:
[HttpGet("{yearId}")]
private async Task<ActionResult<Student>> GetYear(long yearId)
{
var student = await _context.Student.FindAsync(yearId);
if (student == null)
{
return NotFound();
}
return student;
}
您可以在UseEndpointRouteResolverMiddleware
上配置API的路由。样品:
//This is the endpoint route resolver middleware
app.UseEndpointRouteResolverMiddleware(routes =>
{
//The route mapping configuration is passed to the endpoint resolution middleware
routes.MapControllers();
})
https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/