我继承了一些ASP.Net MVC代码并负责添加一些新功能。我是一个使用ASP.Net MVC的完整初学者,主要来自使用Web Forms的背景。MVC操作导致404结果为“未找到与控制器匹配的类型”调用控制器作品的默认操作
我添加了一个新的控制器(ApiController),我增加了以下行动,以它:
// GET: /Api/Index
public string Index()
{
return "API Methods";
}
// GET: /Api/DetectionActivity
public JsonResult DetectionActivity()
{
var detections = from d in db.Detections
orderby DbFunctions.TruncateTime(d.CreationTime)
group d by DbFunctions.TruncateTime(d.CreationTime) into g
select new { date = g.Key, count = g.Count() };
ViewBag.DetectionCounts = detections.ToList();
return Json(detections, JsonRequestBehavior.AllowGet);
}
我RouteConfig.cs具有以下注册路线。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
这看起来像我一直在阅读的教程,但它不工作,我可能错过了一些东西。
如果我去本地主机:21574/api我看到Index()操作,“API方法”的输出。
如果我去为localhost:21574/API/DetectionActivity它抛出与在响应下列数据的404:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'DetectionActivity'."
}
我想有一些我需要做的,我不是。
关于下一步做什么的建议?
更新1
我与我的RouteConfig.cs
routes.MapRoute(name: "ApiController",
url: "{controller}/{action}"
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
想这这些都是我的结果:
如果我去为localhost:21574/API我看到从输出Index()操作,“API方法”。和之前一样。
如果我去为localhost:21574/API/DetectionActivity它抛出与在响应下列数据的404:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'DetectionActivity'."
}
以前一样。
如果我去为localhost:21574 /原料药/原料药/ DetectionActivity它抛出这个数据在响应404:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'Api'."
}
现在,它说,它不能找到一个名为“API”控制器。
这是否申请也定义WebAPI路线?通常URL的“/ api”部分是保留的,可能会导致与'ApiController'混淆。 – David 2014-09-10 19:05:36
用于ApiController的BTW在WebApiConfig类中完成的路由配置不是RouteConfig。cs – aifarfa 2014-09-10 19:16:41
我没有使用WebAPI路由,因为我还没有做过任何研究/阅读。但这听起来可能值得做。 – amber 2014-09-10 19:20:11