VS2012 Web API:未找到与名为n#39的控制器匹配的类型; CpbOutcome n#39;

时间:2012-11-02 14:18:00

标签: asp.net-web-api-routing

有人可以帮忙吗?我有这个控制器:

public class CbpOutcomeController : ApiController
{
    // POST /api/CbpOutcome/1/ 
    public HttpResponseMessage PostCreateCbpOutcome(CbpOutcome co)
    {
        ... snip ...
        return resp_msg;
    }
}

这条路线:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "HubProfilePostRoute", // Route name
            "hub/{controller}/{action}", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );

        routes.MapHttpRoute(
            name: "ProfileByRegionApi",
            routeTemplate: "api/{controller}/Region/{region_name}"
        );

        routes.MapHttpRoute(
            name: "ProfileByGlobalPriorityApi",
            routeTemplate: "api/{controller}/GlobalPriority/{priority_name}"
        );

        routes.MapHttpRoute(
            name: "ApiRoute",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

当我使用此JSON结构制作POSThttp://localhost:2515/api/CpbOutcome时:

{ "Id":0, "Description":"This is a description", "DisplayOrder": 3, 
  "ModifiedBy":"Somebody's name here", "Cbp": { "Id": 163 }}

我收到此消息:

No type was found that matches the controller named 'CpbOutcome'.

1 个答案:

答案 0 :(得分:5)

--- 修改 ---

查看API类的拼写。

public class CbpOutcomeController : ApiController

然后查看您网址的拼写:

http://localhost:2515/api/CpbOutcome

注意Cbp和Cpb

--- 结束编辑 ---

API控制器URL应位于WebApiConfig.cs文件中。

不在RouteConfig.cs文件

为Web API项目分析VS2012提供的默认模板,您将看到它。