Ajax调用web API成功,但未找到C#调用web API 404
我有以下web API方法:Ajax调用web API成功,但未找到C#调用web API 404,c#,asp.net-web-api,C#,Asp.net Web Api,我有以下web API方法: [System.Web.Http.RoutePrefix("api/PurchaseOrder")] public class PurchaseOrderController : ApiController { private static ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[System.Web.Http.RoutePrefix("api/PurchaseOrder")]
public class PurchaseOrderController : ApiController
{
private static ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[System.Web.Http.Route("PagingCriticalPart")]
[System.Web.Http.HttpPost]
public JsonResult PagingCriticalPart([FromBody] Helper.DataTablesBase model)
{
logger.Info("PagingCriticalPart");
JsonResult jsonResult = new JsonResult();
try
{
if (model == null) { logger.Info("model is null."); }
int filteredResultsCount;
int totalResultsCount;
var res = BLL.PurchaseOrderHandler.PagingCriticalPart(model, out filteredResultsCount, out totalResultsCount);
var result = new List<Models.T_CT2_CriticalPart>(res.Count);
foreach (var s in res)
{
// simple remapping adding extra info to found dataset
result.Add(new Models.T_CT2_CriticalPart
{
active = s.active,
createBy = s.createBy,
createdDate = s.createdDate,
id = s.id,
modifiedBy = s.modifiedBy,
modifiedDate = s.modifiedDate,
partDescription = s.partDescription,
partNumber = s.partNumber
});
};
jsonResult.Data = new
{
draw = model.draw,
recordsTotal = totalResultsCount,
recordsFiltered = filteredResultsCount,
data = result
};
return jsonResult;
}
catch (Exception exception)
{
logger.Error("PagingCriticalPart", exception);
string exceptionMessage = ((string.IsNullOrEmpty(exception.Message)) ? "" : Environment.NewLine + Environment.NewLine + exception.Message);
string innerExceptionMessage = ((exception.InnerException == null) ? "" : ((string.IsNullOrEmpty(exception.InnerException.Message)) ? "" : Environment.NewLine + Environment.NewLine + exception.InnerException.Message));
jsonResult.Data = new
{
draw = model.draw,
recordsTotal = 0,
recordsFiltered = 0,
data = new { },
error = exception.Message
};
return jsonResult;
}
}
[System.Web.Http.Route("UploadRawMaterialData")]
[System.Web.Http.HttpPost]
public JsonResult UploadRawMaterialData(string rawMaterialSupplierData)
{
JsonResult jsonResult = new JsonResult();
jsonResult.Data = new
{
uploadSuccess = true
};
return jsonResult;
}
}
但是当从c#调用UploadRawMaterialData时,它得到错误:404未找到
var data = Newtonsoft.Json.JsonConvert.SerializeObject(rawMaterialVendorUploads);
string apiURL = @"http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL);
request.UseDefaultCredentials = true;
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
{
requestWriter.Write(data);
}
try
{
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
}
}
catch (Exception exception)
{
}
使用邮递员返回类似错误:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData'.",
"MessageDetail": "No action was found on the controller 'PurchaseOrder' that matches the request."
}
但如果我用邮递员这样称呼它,没有问题:
http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData?rawMaterialSupplierData=test
我遗漏了什么?在您的
UploadRawMaterialData
方法的方法签名中,您遗漏了[FromBody]
属性。对于UploadRawMaterialData
方法,所有在正文中包含数据的POST请求都需要此在方法签名中,您缺少[FromBody]
属性。所有在正文中包含数据的POST
请求都需要这个您有两个选择
按照其他答案中的建议使用[FromBody]
让你的Url像这样
string queryData=“test”
字符串apiUrl=”http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData?rawMaterialSupplierData=“+试验代码>
基本上,
发送查询数据的方式才是最重要的,如果不指定[FromBody]属性,数据将在URI中传递,并且必须修改URI。您有两个选项
按照其他答案中的建议使用[FromBody]
让你的Url像这样
string queryData=“test”
字符串apiUrl=”http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData?rawMaterialSupplierData=“+试验代码>
基本上,
发送查询数据的方式才是最重要的,如果不指定[FromBody]属性,数据将在URI中传递,URI必须修改。您的ajax和C#代码没有调用相同的方法,请尝试从ajax调用相同的方法,看看是否得到一个404,而且您似乎缺少了[FromBody]
在UploadRawMaterialData
方法签名中您的ajax和C#代码没有调用同一个方法,请尝试从ajax调用同一个方法,看看您是否得到了一个404,而且您似乎缺少UploadRawMaterialData
方法签名中的[FromBody]
http://localhost/ControlTower2WebAPI/api/PurchaseOrder/UploadRawMaterialData?rawMaterialSupplierData=test