5
我有下面的代码,但请求端(富()/酒吧())总是在No action was found on the controller 'Device' that matches the request.
ASP.NET的WebAPI - 没有行动被发现
我在WebApiConfig已经定制路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional}
);
我的ASP.NET的WebAPI控制器:
[HttpPost]
public void UpdateToken(string newToken)
{
_deviceHandler.UpdateToken(newToken);
}
要查询我的ASP.NET的WebAPI我使用RestSharp。
private static void Send(string resource, Method method, object payload)
{
var client = new RestClient(baseUrl);
var request = new RestRequest(resource, method);
request.XmlSerializer = new JsonSerializer();
request.RequestFormat = DataFormat.Json;
request.AddBody(payload);
var response = client.Execute(request);
// ... handling response (exceptions, errors, ...)
}
public void Foo()
{
var newToken = "1234567890";
Send("/api/device/updatetoken", RestSharp.Method.POST, newToken);
}
public void Bar()
{
var newToken = new { newToken = "1234567890" };
Send("/api/device/updatetoken", RestSharp.Method.POST, newToken);
}
避免这种错误的唯一方法是用一个属性创建一个包装类(获取;集;),它具有控制参数(newToken)的名称。
我有很多请求发送一个或两个自定义字符串(未定义的长度)作为后(get的长度有限)。但是为每个场景创建一个包装器实现是真正的开销!我正在寻找另一条路。
PS:我希望我没有通过简化方案所做的任何错误=)
谢谢,只是缺少[FromBody] - 属性是错误。 – dannyyy 2013-02-28 14:24:54
这只是拯救了我的夜晚。我在b my我的脑袋,想知道为什么这一条特定的路线变成了404.这需要一个int,其余的路线都采用参考类型。添加[FromBody]属性的窍门。 – squillman 2015-04-12 06:09:59
1为了挽救我半天的生命,一半已经浪费在这里谢谢 – 2016-07-09 08:02:08