找到多个符合请求的操作:***
Multiple actions were found that match the request:***
我正在尝试从 postman 调用 WEBAPI。我的 AI 方法是 post 但是当我执行它时,我得到以下错误
Multiple actions were found that match the request:***
下面是我的代码:
网络api route.config
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Api Default",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
API控制器:
public class MembershipController : ApiController
{
[System.Web.Http.ActionName("Upload")]
public HttpResponseMessage Upload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.ActionName("BulkUpload")]
[System.Web.Http.HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.ActionName("Transfer")]
public HttpResponseMessage Transfer(string employeedetails)
{
`Some Code`
}
}
我没弄错方法有不同的操作名称和路由配置也是完全合格的 api url 其中包含控制器方法和 id 作为可选 parameter.To 识别 url 这应该足够了,但它不起作用。
我错过了什么吗?
如果这是 RESTful API,你不能有三个 HttpPost
,除非你用 URL 鼻涕虫来区分它们。
最简单的方法是使用属性路由。例如
public class MembershipController : ApiController
{
// POST api/Membership/data/Upload
[Route("api/Membership/{employeedetails}/Upload")]
[HttpPost]
public HttpResponseMessage Upload(string employeedetails)
{
`Some Code`
}
// POST api/Membership/data/Bulk
[Route("api/Membership/{employeedetails}/Bulk")]
[HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
// POST api/Membership/data/Transfer
[Route("api/Membership/{employeedetails}/Transfer")]
[HttpPost]
public HttpResponseMessage Transfer(string employeedetails)
{
`Some Code`
}
}
解决方案一:
我在WebApiConfig中添加了Route Config class
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Membership",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Api Default",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
解决方案 2
public class MembershipController : ApiController
{
[System.Web.Http.ActionName("Upload")]
public HttpResponseMessage Upload([FromBody]string employeedetails)
{
`Some Code`
}
[System.Web.Http.HttpRoute("api/membeship/BulkUpload")]
[System.Web.Http.HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.HttpRoute("api/membeship/Transfer")]
public HttpResponseMessage Transfer([FromBody]string employeedetails)
{
`Some Code`
}
}
如果我们比较解决方案 1 和解决方案 2,那么解决方案 1 将起作用,但它需要一个查询字符串参数,而解决方案 2 也适用于 post 参数(FormBody)
我正在详细了解解决方案 2 有何不同。因为当我们从解决方案 2 中删除 HTTPRoute 时,它也只需要查询字符串参数,如果我们尝试使用 post 传递参数,那么它会作为空值传递。很快我会找到它并分享关于堆栈溢出的详细分析。
快乐编码
我正在尝试从 postman 调用 WEBAPI。我的 AI 方法是 post 但是当我执行它时,我得到以下错误
Multiple actions were found that match the request:***
下面是我的代码: 网络api route.config
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Api Default",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
API控制器:
public class MembershipController : ApiController
{
[System.Web.Http.ActionName("Upload")]
public HttpResponseMessage Upload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.ActionName("BulkUpload")]
[System.Web.Http.HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.ActionName("Transfer")]
public HttpResponseMessage Transfer(string employeedetails)
{
`Some Code`
}
}
我没弄错方法有不同的操作名称和路由配置也是完全合格的 api url 其中包含控制器方法和 id 作为可选 parameter.To 识别 url 这应该足够了,但它不起作用。 我错过了什么吗?
如果这是 RESTful API,你不能有三个 HttpPost
,除非你用 URL 鼻涕虫来区分它们。
最简单的方法是使用属性路由。例如
public class MembershipController : ApiController
{
// POST api/Membership/data/Upload
[Route("api/Membership/{employeedetails}/Upload")]
[HttpPost]
public HttpResponseMessage Upload(string employeedetails)
{
`Some Code`
}
// POST api/Membership/data/Bulk
[Route("api/Membership/{employeedetails}/Bulk")]
[HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
// POST api/Membership/data/Transfer
[Route("api/Membership/{employeedetails}/Transfer")]
[HttpPost]
public HttpResponseMessage Transfer(string employeedetails)
{
`Some Code`
}
}
解决方案一:
我在WebApiConfig中添加了Route Config class
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Membership",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Api Default",
routeTemplate: "api/{controller}/{method}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
解决方案 2
public class MembershipController : ApiController
{
[System.Web.Http.ActionName("Upload")]
public HttpResponseMessage Upload([FromBody]string employeedetails)
{
`Some Code`
}
[System.Web.Http.HttpRoute("api/membeship/BulkUpload")]
[System.Web.Http.HttpPost]
public HttpResponseMessage BulkUpload(string employeedetails)
{
`Some Code`
}
[System.Web.Http.HttpRoute("api/membeship/Transfer")]
public HttpResponseMessage Transfer([FromBody]string employeedetails)
{
`Some Code`
}
}
如果我们比较解决方案 1 和解决方案 2,那么解决方案 1 将起作用,但它需要一个查询字符串参数,而解决方案 2 也适用于 post 参数(FormBody)
我正在详细了解解决方案 2 有何不同。因为当我们从解决方案 2 中删除 HTTPRoute 时,它也只需要查询字符串参数,如果我们尝试使用 post 传递参数,那么它会作为空值传递。很快我会找到它并分享关于堆栈溢出的详细分析。
快乐编码