【发布时间】:2019-07-31 16:39:05
【问题描述】:
好的,当我们第一次运行一个web api项目时,我们通常在WebApiConfig中有这个
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
如果我想创建一个新路由而不是使用 {controller} 会是这样的
config.Routes.MapHttpRoute(
"DefaultApi",
"api/queries/{query}/{id}",
new { id = RouteParameter.Optional }
);
我有我的自定义类来检索这样的名单:
public class GetNameListQuery
{
[HttpGet]
public IEnumerable<String> GetNames(){
return new String[] { "John" , "Adams" };
}
}
因此,当我运行 URI“mylocalhost/api/queries/GetNameList”时,我会得到名称“John”和“Adams”。我是 .NET 的新手,不确定有什么方法可以做这样的事情。感谢你的帮助。谢谢!!
【问题讨论】:
-
在 4.5 或 4.6+ MVC 中允许
[HttpGet]拥有路由的 URL。请参阅:docs.microsoft.com/en-us/aspnet/core/mvc/controllers/… 中带有 Http[Verb] 属性的属性路由以及上面的段落 - 这对您有用吗?阅读整篇文章:)
标签: c# asp.net api asp.net-web-api asp.net-web-api-routing