【发布时间】:2016-04-08 10:13:27
【问题描述】:
我已经在我的项目中添加了一个 webapi 2 控制器,在 api > LoginAPi 中,如下所示:
在 LoginApi 中我有以下内容:
[RoutePrefix("api/LoginApi")]
public class LoginApi : ApiController
{
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
}
在我的 global.asax 文件中,我有:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
在 App_Start 里面我有以下内容:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
然后我在 LoginAPI 的 Get 方法中放置一个断点并运行项目并在 URL 中键入以下内容:
http://localhost:37495/api/LoginApi/4
但我得到:
No HTTP resource was found that matches the request URI 'http://localhost:37495/api/LoginApi/4'.
所以我想好吧,让我这样指定方法名称
http://localhost:37495/api/LoginApi/Get/4
这会返回:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
现在我已经研究了一段时间,所以也许我错过了一些明显的东西,但如果有人能告诉我我做错了什么,我将非常感激。
【问题讨论】:
标签: asp.net-web-api2 asp.net-mvc-routing url-routing