【问题标题】:No action was found on the controller 'Tickets' that matches the name 'CloseTicket'在与名称“CloseTicket”匹配的控制器“Tickets”上未找到任何操作
【发布时间】:2014-09-22 22:30:12
【问题描述】:

我正在编写一个 webapi 项目,但收到错误:当我尝试控制器中的任何方法时,在控制器“Tickets”上找不到与名称“TestMethod”匹配的操作。

以下控制器中的所有操作都不起作用。

我一直在谷歌搜索,并且已经设置了 WebApiConfig.cs 以向路由添加操作。

还有什么我想念的吗?

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();
    }
}

控制器

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;
    }
}

【问题讨论】:

  • @ValutBoy14 当您收到此错误时,您的 URL 是什么样的?
  • 您可以使用属性路由使事情变得清晰。此外,您必须确保使用 System.Web.Http 命名空间作为 HttpPost 属性。
  • 您使用什么 url 来获取“未找到任何操作”错误?为什么你的所有动作中都有static?表单发布 url 需要是 yoursite.tld/api/ticket/IssueTicket
  • 从您的操作方法中删除static

标签: c# asp.net-mvc asp.net-web-api-routing


【解决方案1】:

Web API 在选择操作时不考虑静态方法。

Reference

控制器上的哪些方法被视为“动作”?选择操作时,框架仅查看控制器上的公共实例方法。

从您的控制器中删除 static 关键字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多