C# 带有2个参数的ASP.NET 2.0 Web API问题
我编写了以下代码以获得一个接受两个参数的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
[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(…)]
属性也指定了路由