web api方法:
[ResponseType(typeof(AspNetUser))]
public IHttpActionResult GetUserId(string userName)
{
AspNetUser aspnetuser = db.AspNetUsers.Find(userName);
if (aspnetuser == null)
{
return NotFound();
}
return Ok(aspnetuser);
}
angularjs contoller:
var promisePost = AssetService.getUserId($scope.userName);
Angularjs服务调用webapi方法: //根据用户名获取用户ID的功能
this.getUserId = function(userName) {
return $http.get("/api/AssetAPI/GetUserId/" + userName);
};
webapi.config:
config.Routes.MapHttpRoute(
name: "DemoAPI",
routeTemplate: "api/{controller}/{action}/{userName}",
defaults: new { userName = string.Empty}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我收到此错误:404找不到错误..请事先感谢帮助。
Request URL:http://localhost:52018/api/AssetAPI/GetUserId/admin
Request Method:GET
Status Code:404 Not Found
答案 0 :(得分:0)
ApiController
班的名字是什么?确保将控制器的名称指定为AssetAPIController
以遵循必要的约定。
除了路由看起来很好之外,如果正确设置了控制器的名称,所提供的代码也能正常工作。