對不起,這個跛腳的問題。我已經閱讀過所有類似的問題,但仍無法解決我的問題。控制器上找不到與請求匹配的動作
我越來越從AJAX調用時錯誤 '沒有行動相匹配的請求的控制器上找到':
$.ajax({
url: '/api/ToyEdit/Post/',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({toyId: 1, toy: 'asd'}),
async: true,
processData: false,
cache: false,
success: function (data) {
alert(data);
},
error: function (xhr) {
alert(xhr.statusText);
}
})
控制器:
public class ToyEditController : ApiController
{
[System.Web.Mvc.HttpGet]
public EditToyViewModel Get(int? toyId)
{
var model = new EditToyViewModel();
if (toyId.HasValue)
{
model.Toy = Data.GetToy(toyId.Value);
}
model.Companies = Data.GetCompanies().ToList();
return model;
}
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.ActionName("Post")]
public ActionResult Post(int? toyId, string toy)
{
var a = toy;
/*new Task<int>(() => DB.Context.Toys.FirstAsync().Id);*/
return new EmptyResult();
}
}
路由:
routes.MapRoute(
name: "WebApi",
url: "api/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
代碼有什麼問題?
更新1:
public class Toy
{
public int? toyId {get; set;}
public string toy {get; set;}
}
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.ActionName("Post")]
public ActionResult Post(Toy toy)
{
// your necessary codes
}
但如何通過一些基本的增值經銷商:
OK,就當我使用下面的代碼工作?
您的maproute與操作不匹配 – gypsyCoder
Web API只能綁定來自HTTP主體的單個基本類型 – gypsyCoder
但在不同的文章,如http://www.codeproject.com/Articles/795483/Do-GET-POST-PUT-DELETE-in-asp-net-MVC-with-Jquery他們使用多個變量。他們都錯了嗎? – LbISS