我试图指出我的行动,基本上我有一个控制器有不同的方法,我想指向不同的方法,当我有一个名为create的方法时,一切运行良好,现在我有一个叫{{1从现在开始忽略最佳实践REST。因此,每当我需要指向createDocument
它失败时,我就会得到这个(邮差)。
createDocument
我只是在我的控制器中有这个:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Multiple actions were found that match the request: \r\nCreate on type server.Controllers.DocumentController\r\nCreateDocument on type server.Controllers.DocumentController",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
它没有进入我试图调试它的控制器,我工作得很好的另一种方法输入没有问题。
public async Task<IHttpActionResult> CreateDocument([FromBody]List<DocumentDetails> details)
{
return Ok(200);
}
我的路线配置如下:
public async Task<IHttpActionResult> Create([FromBody]AppDocument document)
{
我怀疑它与我收到的参数有关,我收到了这样的jsonArray:
namespace server
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
为此,我创建了2个模型,第一个具有简单列表
[
{
"key": "Table",
"rows": 1,
"cols": 1,
"cells": 1
},
{
"key": "Paragraph",
"text": "asda",
"fontSize": 14
},
{
"key": "Paragraph",
"text": "asda",
"fontSize": 14
},
{
"key": "Paragraph",
"text": "asda",
"fontSize": 14
}
]
在列表元素中包含可能道具的第二个
namespace server.Models
{
public class DocumentDetails
{
public int Id { get; set; }
public List<DocumentAtributes> atributes { set; get; }
}
}
答案 0 :(得分:1)
更新WebApiConfig
路由模板以识别路由模板中的操作名称,因为基于默认约定的路由模板通常为api/{controller}/{id}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
您还应该使用[HttpPost]
属性更新操作。
public class DocumentController : ApiController {
[HttpPost]
public async Task<IHttpActionResult> CreateDocument([FromBody]List<DocumentDetails> details) {
//...
}
[HttpPost]
public async Task<IHttpActionResult> Create([FromBody]AppDocument document) {
//...
}
}
答案 1 :(得分:0)
问题可能出在您调用api的js代码中。我相信你应该声明一个方法应该如何接收请求。
示例:
[HttpPost]
public async Task<IHttpActionResult> CreateDocument([FromBody]List<DocumentDetails> details)
{
return Ok();
}
添加HttpPost属性使CreateDocument方法仅接收发布请求。