【发布时间】:2015-05-06 14:40:41
【问题描述】:
更新:我卸载了该项目并重新进行了一次,它成功了。
我正在尝试创建一个 WebApi,我的构建工作正常,当我尝试转到 URI 时收到错误消息“找不到与命名控制器匹配的类型”
这就是我的 webapiconfig 的样子,
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
下面是我的控制器,
public class ClientController : ApiController
{
public List<Client> Get()
{
ICRepository repository = new CRepository(new CContext());
return repository.GetAllClients().ToList();
}
这就是我的 global.asax.cs 文件的样子,
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
我正在尝试浏览网址“http://localhost:50662/api/client”
完整的错误如下,
此 XML 文件似乎没有任何关联的样式信息。文档树如下所示。 未找到与请求 URI“http://localhost:50662/api/Client”匹配的 HTTP 资源。未找到与名为“Client”的控制器匹配的类型。
我的问题与它被标记为重复的问题不同,问题只使用控制器,它是 MVC,我的是“ApiController”,我在创建它之前确实读过它。此外,标记的答案与我在这里的答案相似,我的问题仍然存在。
任何帮助将不胜感激。我真的迷路了。
【问题讨论】:
-
嗨杰拉德 - 我的问题不是重复的。在我打开它之前,我确实看过那个帖子。你能再看看吗?
-
将路由更改为 api/{controller}/{id}。您的路由模板不会告诉 WebAPI URL 的哪一部分应该包含控制器名称。
-
您要么需要将
{controller}添加到routeTemplate 字符串,要么将controller="Client"添加到默认匿名对象。 -
是的。我正在这样做。我也在帖子中更新了它。