简体   繁体   中英

404 Not Found for Web API Route

I make a call to an ASP.Net Web API which returns 404 Not found. The controller is recognized but the action is not found.

I have tried using attribute routing and [HttpGet] for the action. I've also tried using standard API routing in the WebAPI config file. I've even tried combining the two.

//[HttpGet]
//[Route("api/User")]
protected IHttpActionResult GetEmployeeHierarchyList()
{
  // ...
  return Json(results);
}
config.Routes.MapHttpRoute(
  name: "GetEmployeeHierarchyListTwo",
  routeTemplate: "api/{controller}/{action}",
  defaults: new { action = "GetEmployeeHierarchyList", }
);
return fetch("/api/User/" + api, {
  method: "GET",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'credentials': 'include'
  }
}).then(response => {
  return response.json();
})

I expect the action to return a string to be handled by the fetch call.

使您的GetEmployeeHierarchyList()方法在控制器中公开。

This should workfine:

    [HttpGet]
    [Route("api/User")]
    public IHttpActionResult GetEmployeeHierarchyList()
    {
      // ...
      return Json(results);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM