【发布时间】:2014-05-01 10:32:05
【问题描述】:
这是我调用自定义 web api 方法的 js 代码..
$.ajax({
url: '/api/AdminAPI/IndustryPost/?industryName='+addNewIndustryName,
type: 'Post',
cache: false,
contentType: 'application/json; charset=utf-8',
success: function (response) {
// response code.
}
});
这工作正常,但如果我使用“数据”标签发送数据,它就不起作用。如下所示。
$.ajax({
url: '/api/AdminAPI/IndustryPost',
type: 'Post',
cache: false,
contentType: 'application/json; charset=utf-8',
data: { 'industryName': addNewIndustryName },
success: function (response) {
// response code.
}
});
WebApiConfig 是 config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
这是我的api方法
[ActionName("IndustryPost")]
public void AddIndustry(string industryName)
{
//code here
}
使用提琴手第二个代码请求结果测试时出现 404 错误。直接从 URL 访问 Web API 方法工作正常。
答案::
在对这个主题进行 R'N'D 之后,我开始知道 Web API 参数绑定存在问题。所以,我们可以使用 [formUri] 标签或者我们可以有一个模型类。像这样
公共类 IndustryName { 公共字符串 Industryname { 获取;放; } }
在web api方法中
public void IndustryPost(IndustryName IndustryName) { // }
jquery 中不需要更改。
这篇文章在这里解释一切 Post int as part of method but get No HTTP resource was found that matches the request URI error
【问题讨论】:
标签: javascript jquery asp.net-web-api