【问题标题】:404 Not Found for Web API Route404 Not Found for Web API 路由
【发布时间】:2019-06-14 15:18:32
【问题描述】:

我调用了一个返回 404 Not found 的 ASP.Net Web API。控制器被识别,但未找到操作。

我尝试使用属性路由和[HttpGet] 进行操作。我还尝试在 WebAPI 配置文件中使用标准 API 路由。我什至尝试将两者结合起来。

//[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();
})

我希望该操作返回一个由 fetch 调用处理的字符串。

【问题讨论】:

    标签: javascript c# jquery asp.net asp.net-web-api-routing


    【解决方案1】:

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

    【讨论】:

    • 天哪,谢谢!好尴尬!
    【解决方案2】:

    这应该可以正常工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2016-07-22
      相关资源
      最近更新 更多