C# “接收”;“未找到资源”;使用HttpClient向WebApi传递对象时
背景 目前有一个api,这是我不久前创建的一个助手api。这个api有一个控制器,它接收一个对象,然后相应地处理它 问题 当我调用我的接收api时,我收到一个错误消息,错误消息如下 {“Message”:“未找到与请求URI匹配的HTTP资源 '',“MessageDetail”:“否 在与匹配的控制器“DataServices”上找到了操作 请求。”} 但是,控制器是可操作的,因为我可以调用控件中的另一个函数,该函数可以为我提供所有客户端。我确保我的路由属性与我的请求url匹配,但仍然没有成功 代码 发送Web ApiC# “接收”;“未找到资源”;使用HttpClient向WebApi传递对象时,c#,asp.net-web-api2,asp.net-web-api-routing,C#,Asp.net Web Api2,Asp.net Web Api Routing,背景 目前有一个api,这是我不久前创建的一个助手api。这个api有一个控制器,它接收一个对象,然后相应地处理它 问题 当我调用我的接收api时,我收到一个错误消息,错误消息如下 {“Message”:“未找到与请求URI匹配的HTTP资源 '',“MessageDetail”:“否 在与匹配的控制器“DataServices”上找到了操作 请求。”} 但是,控制器是可操作的,因为我可以调用控件中的另一个函数,该函数可以为我提供所有客户端。我确保我的路由属性与我的请求url匹配,但仍然没有成功
public static async Task<bool> CreateDBF(string dPath)
{
var postObject = (new DBFPostModel
{
destinationPath = dPath,
fileName = "CustomDBF"
});
List<string> uniqueID = new List<string>();
try
{
string requestUrl = "http://appdev.tenant.com/caseware32/api/DataServices/CreateDBF";
HttpClient hc = new HttpClient();
var method = new HttpMethod("POST");
var values = new Dictionary<string, string>()
{
{ "Id", "6"},
{ "Name", "Skis"},
{ "Price", "100"},
{ "Category", "Sports"}
};
var content = new FormUrlEncodedContent(values);
var hrm = await hc.PostAsync(requestUrl, content);
if (hrm.IsSuccessStatusCode)
{
var responseData = await hrm.Content.ReadAsStringAsync();
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
// TODO Log
return false;
}
}
public class IncomingDbfModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public string Catergory { get; set; }
}
public class DataServicesController : ApiController
{
[Route("api/DataServices/CreateDBF")]
public IHttpActionResult CreateDBF(IncomingDbfModel postParam)
{
DatabaseServices dbServices = new DatabaseServices();
bool success = dbServices.InsertDataIntoDBF(postParam);
return Ok(success);
}
}
错误
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 30 Mar 2017 15:17:38 GMT
Content-Length: 232
{"Message":"No HTTP resource was found that matches the request URI 'http://appdev.tenant.com/caseware32/api/DataServices/CreateDBF'.","MessageDetail":"No action was found on the controller 'DataServices' that matches the request."}
路由配置
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
log4net.Config.XmlConfigurator.Configure();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
默认情况下,web api基于约定的路由模板不包括URL模板
“api/{controller}/{id}”
中的操作名称。上面的示例包括requestUrl=”中的操作名称http://appdev.tenant.com/caseware32/api/DataServices/CreateDBF“
与属性路由匹配<代码>[路由(“api/DataServices/CreateDBF”)]
因此,假设您打算使用属性路由
将HttpPost
属性应用于操作,以便route知道如何处理POST请求
public class DataServicesController : ApiController {
//POST api/DataServices/CreateDBF
[HttpPost]
[Route("api/DataServices/CreateDBF")]
public IHttpActionResult CreateDBF(IncomingDbfModel postParam) {
DatabaseServices dbServices = new DatabaseServices();
bool success = dbServices.InsertDataIntoDBF(postParam);
return Ok(success);
}
}
资料来源:
Web API还根据请求的HTTP方法选择操作
(获取、发布等)。默认情况下,Web API查找不区分大小写的
与控制器方法名称的开头匹配
您可以通过使用任何
以下属性:
[HttpDelete]
[HttpGet]
[HttpHead]
[HttpOptions]
[HttpPatch]
[HttpPost]
[HttpPut]
你的RouteConfig是什么样子的?@maccettura我发布了路由配置你是否尝试过用
HttpPost
@EasyE标记你的操作配置是针对MVC而不是Web API的。这将出现在WebApiConfig.cs中file@Nkosi是的,你是对的,我已经更新了,谢谢你指出这一点。