Search code examples
c#asp.net-mvcasp.net-web-api-routing

No action was found on the controller 'Tickets' that matches the name 'CloseTicket'


I am writing a webapi project and am receiving the error No action was found on the controller 'Tickets' that matches the name 'TestMethod' whenever I try any of the methods in the controller.

None of the actions in the below controller are working.

I have been googling and have already setup WebApiConfig.cs to add action to the routing.

Is there something else I am missing.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });

        config.EnableSystemDiagnosticsTracing();
    }
}

Controller

public class TicketController : ApiController
{
    [HttpPost]
    public static ServiceResponse<string> IssueTicket([FromBody]ServiceRequest<TicketRequest> request)
    {            
        return ServiceResponse<string>.WithPayload(ticketID);
    }

    [HttpPost]
    public static ServiceResponse<bool> CheckTicketExist([FromBody]ServiceRequest<string> request)
    {            
        return ServiceResponse<bool>.WithPayload(doesExist);
    }

    [HttpPost]
    public static ServiceResponse<bool> CloseTicket([FromBody]ServiceRequest<string> request)
    {
        return ServiceResponse<bool>.WithPayload(result);
    }

    [HttpPost]
    public static bool TestMethod([FromBody]string test)
    {
        return true;
    }
}

Solution

  • Web API doesn't consider static methods while selecting actions.

    Reference

    Which methods on the controller are considered "actions"? When selecting an action, the framework only looks at public instance methods on the controller.

    Remove the static keyword from your controller.