我需要创建一个ASP.NET Core, Web API支持多个HttpGet动词的唯一区别是查询字符串,但似乎查询字符串不能成为路由模板的一部分 - 是真的吗?
路径模板非常相似,实际上它们只有查询字符串不同.
[Authorize]
public class SymbolsController : Controller
{
[
HttpGet,
Route("api/symbols")
]
public Task<IEnumerable<Symbol>> Symbols([FromServices] ISymbolService symbolService)
{
return symbolService.GetSymbolsAsync();
}
[
HttpGet,
Route("api/symbols?{childrenOf=id}")
]
public Task<IEnumerable<Symbol>> ValidChildren(
[FromQuery] Guid id,
[FromServices] ISymbolService symbolService)
{
return symbolService.GetValidChildrenAsync(id);
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发异常,因为?它不是路由模板中的有效字符.我怎样才能做到这一点?
routing asp.net-web-api asp.net-web-api2 asp.net-core asp.net-core-webapi
我正在我的Web API帮助页面中呈现重复条目,其中包含不同的父级,例如这些,引用相同的方法:
GET api/{apiVersion}/v1/Products - 获取所有产品
...
GET api/v1/Products - 获取所有产品
...
我有一个Web API页面,其中包含一些这样的路由:
config.Routes.MapHttpRoute (
name: "DefaultVersionApi",
routeTemplate: "api/{apiVersion}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute (
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我原以为这个路由会使"v1"成为可选项,所以上面的派生文档是不可取的.
(侧边栏:api/products肯定不行,所以我不确定这有什么问题.我错过了什么?)
看起来真正的问题是Web API帮助页面正在不正确地读取路由,正如我想的那样v1,{apiVersion} 不应该同时出现在同一个动作中. 我在这里错过了什么?
asp.net-mvc-routing asp.net-web-api asp.net-web-api-helppages