Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为{id}/访问创建有效的路由?_C#_Asp.net Core_Asp.net Core Webapi_Asp.net Core Routing - Fatal编程技术网

C# 如何为{id}/访问创建有效的路由?

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) {

我是asp.core的新手,所以我尝试通过有效的路径访问
{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
    }
}