在控制器 'User' 上找不到与名称 'Name' 匹配的操作
No action was found on the controller 'User' that matches the name 'Name'
我的控制器中有两个操作方法。
[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()
同时更新控制器。尽量不要在同一控制器中混合基于约定和属性的路由
[RoutePrefix("user")]
public class UserController: ApiController
{
//GET user
[Route("")]
[HttpGet]
public IEnumerable<User> Get() { ... }
//GET user/Jane
[Route("{name}")]
[HttpGet]
public IEnumerable<User> GetByName(string name) { ... }
}
我的控制器中有两个操作方法。
[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()
同时更新控制器。尽量不要在同一控制器中混合基于约定和属性的路由
[RoutePrefix("user")]
public class UserController: ApiController
{
//GET user
[Route("")]
[HttpGet]
public IEnumerable<User> Get() { ... }
//GET user/Jane
[Route("{name}")]
[HttpGet]
public IEnumerable<User> GetByName(string name) { ... }
}