[英]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的其他部分?
您可以使用諸如{*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
映射到上述操作。
請注意,這還包括spiceusers
模板下的所有子調用,前提是這是預期的。
另請注意,如果此 Web api 與默認 MVC 一起使用,則路徑與默認路由發生沖突。 但是考慮到 wep api 路由往往在 MVC 路由之前注冊,這可能不是什么大問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.