AJAX
$.ajax({
url: '../api/TechViews/SView',
type: 'POST',
dataType: 'json',
data: JSON.stringify({ Filter: JSON.stringify(eth.Data), Type: $('.productType.active').data('name'), Name: $('#txtView').val() }),
global: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('success');
},
error: function (xhrequest, ErrorText, thrownError) {
alert('Fail');
}
});
控制器
[Route("SView")]
[HttpPost]
public string Post([FromBody]TechViewsModel value)
{
string result = string.Empty;
return result;
}
[Route("DView")]
[HttpPost]
public string Post([FromBody]TechViewsModel value)
{
string result = string.Empty;
return result;
}
Route
使用的命名空间是AttributeRouting.Web.Mvc
。在AJAX
来电时,我得到2个错误500 (Internal Server Error)
TechViews
SView
,回复为"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nSystem.String Post
RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "LoginPage", id = UrlParameter.Optional }
);
}
我试过了,
HttpPost
HttpPost
和Route
FromBody
当我只使用一个没有Route
的帖子时,相同的代码工作正常。
我哪里错了?
答案 0 :(得分:0)
在api路由配置中启用config.MapHttpAttributeRoutes()
选项。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
在global.asax.cs
中GlobalConfiguration.Configure(WebApiConfig.Register);