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# 带有2个参数的ASP.NET 2.0 Web API问题_C#_Asp.net Core_Asp.net Core Webapi_Asp.net Core Routing - Fatal编程技术网

C# 带有2个参数的ASP.NET 2.0 Web API问题

C# 带有2个参数的ASP.NET 2.0 Web API问题,c#,asp.net-core,asp.net-core-webapi,asp.net-core-routing,C#,Asp.net Core,Asp.net Core Webapi,Asp.net Core Routing,我编写了以下代码以获得一个接受两个参数的web api: [Route("api/[controller]")] [ApiController] public class EventsController : ControllerBase { // GET: api/events/5 [Route("api/[controller]/{deviceId}/{action}")] [HttpGet("{deviceId}/{action}")] public IEn

我编写了以下代码以获得一个接受两个参数的web api:

[Route("api/[controller]")]
[ApiController]
public class EventsController : ControllerBase
{
    // GET: api/events/5
    [Route("api/[controller]/{deviceId}/{action}")]
    [HttpGet("{deviceId}/{action}")]
    public IEnumerable<CosmosDBEvents> Get(string deviceId,string action)
    {
        try
        {
            return null;
        }
        catch(Exception ex)
        {
            throw (ex);
        }
    }
}
但它不起作用。错误是404,找不到页面

我还更改了代码,如下所示,但没有任何更改:

[Route("~/api/events/{deviceId}/{action}")]
[HttpGet("{deviceId}/{action}")]
public IEnumerable<CosmosDBEvents> Get(string deviceId,string action)
{
    try
    {
        return null;
    }
    catch(Exception ex)
    {
        throw (ex);
    }
}
[路由(“~/api/events/{deviceId}/{action}”)]
[HttpGet(“{deviceId}/{action}”)]
公共IEnumerable Get(字符串设备ID,字符串操作)
{
尝试
{
返回null;
}
捕获(例外情况除外)
{
投掷(ex);
}
}

我做错了什么?

当使用ASP.NET核心路由时,有几个。清单如下:

  • 行动
  • 区域
  • 控制器
  • 处理者
  • 页面
如您所见,
action
在列表中,这意味着您不能将其用于自己的目的。如果您将
操作
更改为列表中未列出的其他操作,它将起作用:

[Route("api/[controller]")]
[ApiController]
public class EventsController : ControllerBase
{
    [HttpGet("{deviceId}/{deviceAction}")]
    public IEnumerable<CosmosDBEvents> Get(string deviceId, string deviceAction)
    {
        // ...
    }
}
[路由(“api/[控制器]”)]
[ApiController]
公共类事件控制器:ControllerBase
{
[HttpGet(“{deviceId}/{deviceAction}”)]
公共IEnumerable Get(字符串设备ID、字符串设备动作)
{
// ...
}
}
请注意,我还从
Get
中删除了
[Route(…)]
属性,这是多余的,因为您使用的
[HttpGet(…)]
属性也指定了路由