使用 HttpClient 将对象传递给 Web Api 时接收 "No resource found"
Receiving "No resource found" when passing an object to a WebApi using HttpClient
背景
目前有一个 api 是我不久前创建的一个助手 api。这个 api 有一个控制器接收一个对象,然后相应地处理它。
问题
当我调用它时,我的接收 api 我收到以下错误消息的错误。
{"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."}
但是,控制器可以运行,因为我可以调用控件中的另一个函数,该函数可以为我提供所有客户端。我确保我的路由属性符合我的请求 url 但仍然没有成功。
代码
发送网页Api
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;
}
}
接收中Api
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"
中包含与属性路由匹配的操作名称。 [Route("api/DataServices/CreateDBF")]
因此假设您打算使用属性路由。
将 HttpPost
属性应用于操作,以便路由知道如何处理 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);
}
}
来源:Attribute Routing in ASP.NET Web API 2 - HTTP Methods
Web API also selects actions based on the HTTP method of the request
(GET, POST, etc). By default, Web API looks for a case-insensitive
match with the start of the controller method name.
You can override this convention by decorating the mathod with any the
following attributes:
[HttpDelete]
[HttpGet]
[HttpHead]
[HttpOptions]
[HttpPatch]
[HttpPost]
[HttpPut]
背景
目前有一个 api 是我不久前创建的一个助手 api。这个 api 有一个控制器接收一个对象,然后相应地处理它。
问题
当我调用它时,我的接收 api 我收到以下错误消息的错误。
{"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."}
但是,控制器可以运行,因为我可以调用控件中的另一个函数,该函数可以为我提供所有客户端。我确保我的路由属性符合我的请求 url 但仍然没有成功。
代码
发送网页Api
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;
}
}
接收中Api
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"
中包含与属性路由匹配的操作名称。 [Route("api/DataServices/CreateDBF")]
因此假设您打算使用属性路由。
将 HttpPost
属性应用于操作,以便路由知道如何处理 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);
}
}
来源:Attribute Routing in ASP.NET Web API 2 - HTTP Methods
Web API also selects actions based on the HTTP method of the request (GET, POST, etc). By default, Web API looks for a case-insensitive match with the start of the controller method name.
You can override this convention by decorating the mathod with any the following attributes:
[HttpDelete] [HttpGet] [HttpHead] [HttpOptions] [HttpPatch] [HttpPost] [HttpPut]