【发布时间】:2019-07-01 20:21:08
【问题描述】:
我有一个关于使用 ASP.NET Web API 2 路由 POST 请求的问题。
我似乎无法调用 POST 函数,它总是返回未找到 404。
{
"Message": "No HTTP resource was found that matches the request URI 'https://....../api/CombinedPOResponse/PostCombinedPOResponse'.",
"MessageDetail": "No action was found on the controller 'CombinedPOResponse' that matches the request."
}
有人能指出我的配置有问题吗? 这是控制器的相关部分
namespace FormSupportService.Controllers
{
public class CombinedPOResponseController : ApiController
{
[HttpPost]
public IHttpActionResult PostCombinedPOResponse(string inputXml)
{
AddPurchaseOrderResponse response = new AddPurchaseOrderResponse();
//...
return Ok(response);
}
//...
}
}
WebApiConfig.cs 提取
// UnitCodeLookup
config.Routes.MapHttpRoute(
name: "CombinedPOResponseApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { inputXml = RouteParameter.Optional }
);
我可以毫无问题地访问所有其他控制器,但这个很棘手。
谢谢
编辑:
我正在使用 javascript 调用服务:
$.ajax("/api/CombinedPOResponse/PostCombinedPOResponse",
{
accepts: "text/html",
data: {inputXml: inputXml},
dataType: 'json',
method: 'POST',
error: error,
success: success
});
【问题讨论】:
-
我不确定,但我认为有时默认情况下,路由会省略 post 部分,您可以尝试仅使用 CombinedPOResponse 调用它吗?
-
现在您正在为控制器 and 操作使用相同的名称。您可能应该将
Post操作命名为Post。
标签: c# asp.net web-services asp.net-web-api2