私は次のコードを持っていますが、リクエストは常に終了します(Foo()/ Bar())No action was found on the controller 'Device' that matches the request.
WebApiConfigにカスタムルートがあります。
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional}
);
私のASP.NETWebAPIコントローラー:
[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; set;)を含むラッパークラスを作成することです。
1つまたは2つのカスタム文字列(未定義の長さ)をpost(getの長さに制限があります)として送信するリクエストがたくさんあります。ただし、シナリオごとにラッパーの実装を作成するのは、実際のオーバーヘッドです。私は行く別の方法を探しています。
PS:シナリオを単純化して間違いを犯していないことを願っています=)