ASP.NET WebAPI - No Actions Found

I have the following code, but the request ends (Foo () / Bar ()) always in No action was found on the controller 'Device' that matches the request.

I have my own route in my WebApiConfig:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new {id = RouteParameter.Optional}
    );

My ASPAP WebAPI controller:

[HttpPost]
public void UpdateToken(string newToken)
{
    _deviceHandler.UpdateToken(newToken);
}

To request my ASP.NET WebAPI, I use 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 );
}

The only way to avoid this error is to create a wrapper class with the property (get; set;) in it that has the name of the controller argument (newToken).

I have a lot of requests that send one or two user strings (undefined length) as a record (get is limited in length). But to create a shell implementation for each scenario is a real cost! I am looking for another way.

PS: I hope I made no mistakes by simplifying the script =)

+5
source share
1

URI. , , [FromBody] :

[HttpPost]
public void UpdateToken([FromBody] string newToken)
{
    _deviceHandler.UpdateToken(newToken);
}

. JSON, :

"1234567890"
+12

All Articles