C# 如何为{id}/访问创建有效的路由?
我是asp.core的新手,所以我尝试通过有效的路径访问C# 如何为{id}/访问创建有效的路由?,c#,asp.net-core,asp.net-core-webapi,asp.net-core-routing,C#,Asp.net Core,Asp.net Core Webapi,Asp.net Core Routing,我是asp.core的新手,所以我尝试通过有效的路径访问{id}/visions 我的代码: [Produces("application/json")] [Route("/Users")] public class UserController { [HttpGet] [Route("{id}/visits")] public async Task<IActionResult> GetUser([FromRoute] long id) {
{id}/visions
我的代码:
[Produces("application/json")]
[Route("/Users")]
public class UserController
{
[HttpGet]
[Route("{id}/visits")]
public async Task<IActionResult> GetUser([FromRoute] long id)
{
throw new NotImplementedException()
}
}
如何制作路线/Users/5/访问nethod?
我应该在GetUser
添加哪些参数?以不同的方式命名方法,并使用约束来避免路由冲突:
[Produces("application/json")]
[RoutePrefix("Users")] // different attribute here and not starting /slash
public class UserController
{
// Gets a specific user
[HttpGet]
[Route("{id:long}")] // Matches GET Users/5
public async Task<IActionResult> GetUser([FromRoute] long id)
{
// do what needs to be done
}
// Gets all visits from a specific user
[HttpGet]
[Route("{id:long}/visits")] // Matches GET Users/5/visits
public async Task<IActionResult> GetUserVisits([FromRoute] long id) // method name different
{
// do what needs to be done
}
}
[产生(“应用程序/json”)]
[RoutePrefix(“用户”)]//此处有不同的属性,而不是开始/斜杠
公共类用户控制器
{
//获取特定用户
[HttpGet]
[路由(“{id:long}”)]//匹配获取用户/5
公共异步任务GetUser([FromRoute]长id)
{
//做需要做的事
}
//获取特定用户的所有访问
[HttpGet]
[路由(“{id:long}/visions”)]//匹配获取用户/5/访问
公共异步任务GetUserVisites([FromRoute]long id)//方法名称不同
{
//做需要做的事
}
}
[Produces("application/json")]
[RoutePrefix("Users")] // different attribute here and not starting /slash
public class UserController
{
// Gets a specific user
[HttpGet]
[Route("{id:long}")] // Matches GET Users/5
public async Task<IActionResult> GetUser([FromRoute] long id)
{
// do what needs to be done
}
// Gets all visits from a specific user
[HttpGet]
[Route("{id:long}/visits")] // Matches GET Users/5/visits
public async Task<IActionResult> GetUserVisits([FromRoute] long id) // method name different
{
// do what needs to be done
}
}