找不到与控制器名称匹配的类型

No type was found that matches the controller name

我有臭名昭著的 Web Api 找不到与控制器名称匹配的类型。在我们对文件系统和 classes 进行了一些重组之前,我一直在使用此 Web 服务,现在我遇到了这个错误。在访问来自同一解决方案中的 class 库的数据 class 时,文件系统默认与控制器和模型文件夹一起使用。我的路线没有改变,所以我认为这不是问题。

这是我的控制器:

namespace CECC.Services.Controllers
{
    public class MeterBlinksController : ApiController
    {
        // Will return all the meters for the date range with blinks >= minimum blinks passed.
        public IEnumerable<JsonMeterBlinksModel> GetBlinks(string startDate, string endDate, int meterNumber)
        {
            return GetBlinksByDateRange();
        }

        public IEnumerable<JsonMeterBlinksModel> GetBlinksByDateRange()
        {
            var conn = new Connection();
            List<JsonMeterBlinksModel> blinksCollection = new List<JsonMeterBlinksModel>();

            string queryString =
                "select meteraccts.meternumber, t1.blinks, scemain.subname, feeder.feedername "
                + "from(select serialnumber, sum(blinkcount) as blinks "
                + "from cecc_processed_blinks "
                + "where trunc(blinkdate) between to_date(:startDate, 'dd-mm-yy') and to_date(:endDate, 'dd-mm-yy') group by serialnumber) t1 "
                + "left join meteraccts on t1.serialnumber = meteraccts.serialnumber "
                + "left join serialnumber on t1.serialnumber = serialnumber.serialnumber "
                + "left join scemain on serialnumber.subid = scemain.subid "
                + "left join feeder on serialnumber.subid = feeder.subid and serialnumber.fdrid = feeder.fdrid  "
                + "where METERNUMBER is not null and SUBNAME is not null and FEEDERNAME is not null and t1.blinks >= 0 order by t1.blinks desc";



            conn.QueryInto<JsonMeterBlinksModel>(ref blinksCollection, queryString, null);

            return blinksCollection;
        }

这是我的路线:

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

最后,我的错误:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:36819/api/getblinks'.","MessageDetail":"No type was found that matches the controller named 'getblinks'."}

您的请求应该是 /api/MeterBlinks 类型的 GET

这个URL:

http://localhost:36819/api/getblinks

会寻找一个名为 GetBLinksController 的控制器,但确实找不到。我想你想这样做:

http://localhost:36819/api/meterblinks

您可能还需要将 GetBlinks 方法重命名为 Get 以映射到默认 GET 请求:

public IEnumerable<JsonMeterBlinksModel> Get(string startDate, string endDate, int meterNumber)
{
    return GetBlinksByDateRange();
}

可以 在 WebAPI 控制器中执行 "named" 操作,但我怀疑您可能需要为此添加路由。但默认情况下,它会查找基于动词的操作(Get、Post 等)。

那是因为你写错了url。应该是host and port + "/api/" + controller name + action name。在你的情况下是:

//localhost:36819/api/MeterBlinks/GetBlinksByDateRange