我想指向我的行動,基本上我有1個控制器有不同的方法,我想指向不同的方法,一切運作良好,當我有1個方法稱爲創建,現在我有一個叫createDocument
(從現在開始忽略最佳實踐REST)。所以每次我需要指向createDocument
它都會失敗,我得到這個(郵差)。多個動作被發現匹配請求C#
{
"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)
{
我的路由配置是這樣的:
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 }
);
}
}
}
我懷疑它是與我收到的參數,我收到jsonArray這樣的:
[
{
"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
}
]
併爲此我創建了2個模型,第一個有一個簡單的列表
namespace server.Models
{
public class DocumentDetails
{
public int Id { get; set; }
public List<DocumentAtributes> atributes { set; get; }
}
}
具有列表元素
namespace server.Models
{
public class DocumentAtributes
{
public int Id { get; set; }
public String key { get; set; }
public String rows { get; set; }
public String cols { get; set; }
public String cells { get; set; }
public String fontSize { get; set; }
public String width { get; set; }
public String height { get; set; }
public String base64 { get; set; }
public String align { get; set; }
public String text { get; set; }
}
}
您顯示的路線錯誤。如果這是web api,那麼我們需要看到'WebApiConfig',它默認使用'api/{controller}/{id}'路徑模板 – Nkosi
您可能需要將'config.MapHttpAttributeRoutes();'添加到'WebApiConfig'中,但是如果我們能看到你的'WebApiConfig'會更好。 – Alisson