【发布时间】:2017-03-30 15:39:36
【问题描述】:
背景
目前有一个我创建了一段时间的 api,它是一个辅助 api。这个 api 有一个控制器,它接收一个对象,然后进行相应的处理。
问题
当我调用这个我的接收 api 时,我收到以下错误消息。
{"Message":"没有找到与请求URI匹配的HTTP资源 'http://appdev.tenant.com/caseware32/api/DataServices/CreateDBF'.","MessageDetail":"否 在与 请求。”}
但是,控制器是可操作的,因为我可以调用控件中的另一个函数,它可以为我提供所有客户端。我确保我的路由属性与我的请求 url 匹配,但仍然没有成功。
代码
发送 Web 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 }
);
}
【问题讨论】:
-
你的 RouteConfig 是什么样的?
-
@maccettura 我发布了路线配置
-
你试过用
HttpPost标记你的动作 -
@EasyE 该配置用于 MVC 而不是 Web API。这将在 WebApiConfig.cs 文件中
-
@Nkosi 是的,你是对的,我已经根据感谢您指出这一点进行了更新。
标签: c# asp.net-web-api2 asp.net-web-api-routing