【发布时间】: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