【发布时间】:2017-01-05 14:15:19
【问题描述】:
我的控制器中有两个操作方法。
[RoutePrefix("user")]
public class UserController: ApiController
{
[HttpGet]
public IEnumerable<User> Get()
{
return new User.GetUsers();
}
[Route("{name}")]
[HttpGet]
public IEnumerable<User> GetByName(string name)
{
return new User.GetUsers(name);
}
}
下面是我的路由配置文件
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { controller = "user", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApiGet",
routeTemplate: "{controller}/{id}",
defaults: new { action = "Get", id= RouteParameter.Optional },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
我正在拨打以下电话
localhost/User - - Working
localhost/User/Jane -- Not working throwing error.
我不确定 API 有什么问题。
【问题讨论】:
-
你需要在基于约定的路由之前启用属性路由
config.MapHttpAttributeRoutes
标签: c# asp.net-web-api asp.net-web-api-routing