我有MVC項目,我已經添加了一個API控制器。在這個API控制器中我已經創建了方法。但是當我試圖從郵遞員或本地主機調用這個API的「http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs」給出以下回應。Api控制器方法沒有調用
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs'.",
"MessageDetail": "No type was found that matches the controller named 'BedfordBrownstoneApi'."
}
我的API控制器如下所示。
public class BedfordBrownstoneApi : ApiController
{
// GET api/<controller>
public int GetAgentId(string username,string password)
{
DataContext db = new DataContext();
var data = db.Set<AgentLogin>().Where(a => a.UserName==username && a.Password==password).SingleOrDefault();
return data.AgentId;
}
}
}
我的WebApiConfig類如下所示。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "GetAgentId" }
);
}
}
試試這個 - 從控制器名稱中刪除'Api'http:// localhost:10133/api/BedfordBrownstone/GetAgentId?username = dgsdgsdgsd&password = sdgsdgs –
在你的Global.asax文件中是否有'GlobalConfiguration.Configure(WebApiConfig.Register);'調用? – Alisson
您沒有在您的api控制器名稱中提及Controller,請查看http://stackoverflow.com/questions/11384552/getting-no-type-was-found-that-matches-the-controller-named-sampleslashbaseser –