web api 中的路由属性忽略部分 uri
Routing attribute in web api ignore part of uri
我有一个控制器
public class SimulatorController : ApiController
{
private SimulatorService _simulatorService;
public SimulatorController()
{
_simulatorService = new SimulatorService();
}
[HttpGet]
[Route("spiceusers")]
public async Task<IHttpActionResult> GetConsumerProductsAsync()
{
var consumerProductsList = await _simulatorService.GetConsumerProductsAsync();
return Ok(consumerProductsList);
}
}
我有 uri
http://comm-rpp-emulator.com/spiceusers/9656796/devicesfuse?includeComponents=true&groupByParentDevice=true&includeChildren=true&limit=50&page=1
我需要处理我的方法
http://comm-rpp-emulator.com/spiceusers
并忽略 uri 的其他部分?
您可以使用 catch-all 路由参数,例如 {*segment}
来捕获 URL 路径的剩余部分。
这里假设属性路由已经启用。
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Web API routes
config.MapHttpAttributeRoutes();
// Other Web API configuration not shown.
}
}
发布示例中的 URL 可以通过使用 catch-all 路由参数匹配到操作,该参数将捕获与模板匹配的 URL 路径的剩余部分
//GET spiceusers/{anything here}
[HttpGet]
[Route("~/spiceusers/{*url}")]
public async Task<IHttpActionResult> GetConsumerProductsAsync() { ... }
现在对 /spiceusers
的任何调用都将按预期映射到上述操作。
请注意,这还包括 spiceusers
模板下的所有子调用,前提是这是预期的。
另请注意,如果此网站 api 与默认 MVC 一起使用,则路径会与默认路由冲突。但考虑到 wep api 路由往往在 MVC 路由之前注册,这可能不是什么大问题。
我有一个控制器
public class SimulatorController : ApiController
{
private SimulatorService _simulatorService;
public SimulatorController()
{
_simulatorService = new SimulatorService();
}
[HttpGet]
[Route("spiceusers")]
public async Task<IHttpActionResult> GetConsumerProductsAsync()
{
var consumerProductsList = await _simulatorService.GetConsumerProductsAsync();
return Ok(consumerProductsList);
}
}
我有 uri
http://comm-rpp-emulator.com/spiceusers/9656796/devicesfuse?includeComponents=true&groupByParentDevice=true&includeChildren=true&limit=50&page=1
我需要处理我的方法
http://comm-rpp-emulator.com/spiceusers
并忽略 uri 的其他部分?
您可以使用 catch-all 路由参数,例如 {*segment}
来捕获 URL 路径的剩余部分。
这里假设属性路由已经启用。
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Web API routes
config.MapHttpAttributeRoutes();
// Other Web API configuration not shown.
}
}
发布示例中的 URL 可以通过使用 catch-all 路由参数匹配到操作,该参数将捕获与模板匹配的 URL 路径的剩余部分
//GET spiceusers/{anything here}
[HttpGet]
[Route("~/spiceusers/{*url}")]
public async Task<IHttpActionResult> GetConsumerProductsAsync() { ... }
现在对 /spiceusers
的任何调用都将按预期映射到上述操作。
请注意,这还包括 spiceusers
模板下的所有子调用,前提是这是预期的。
另请注意,如果此网站 api 与默认 MVC 一起使用,则路径会与默认路由冲突。但考虑到 wep api 路由往往在 MVC 路由之前注册,这可能不是什么大问题。