Web API一种操作有效,而几乎完全相同吗?

时间:2019-03-29 17:37:13

标签: asp.net-web-api2 asp.net-web-api-routing

错误消息

{
    "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44390/api/UserRoutes?effectiveDate=3/29/2019'.",
    "MessageDetail": "No type was found that matches the controller named 'UserRoutes'."
}

工作措施

public class AdvanceOrderApiController : BaseApiController
{
    [HttpGet, Route("api/AdvanceOrders")]
    public AdvanceOrdersResult GetAdvanceOrdersForRouteDate(string route, DateTime effectiveDate)
    {
        ...
    }
}

// JavaScript Usage: route="0100" and effectiveDate="03/29/2019".
API.SendRequest("/api/AdvanceOrders", "GET", { route: route, effectiveDate: effectiveDate }, success, failure);

不起作用

public class UserApiController : BaseApiController
{
    [HttpGet, Route("api/UserRoutes")]
    public IEnumerable<string> GetUserRoutes(DateTime effectiveDate)
    {
        ...
    }
}

// JavaScript Usage: effectiveDate="03/29/2019"
API.SendRequest("/api/UserRoutes", "GET", { effectiveDate: effectiveDate }, success, failure);

WebApiConfig

由于我只是声明每个动作的路线,因此不确定是否相关,但是...

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        ...

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

API.SendRequest

该函数只是jQuery $.ajax函数的包装,没有花哨的地方。如果需要代码,我将介绍它,但它可用于所有其他API调用,因此我无法想象它会成为问题的根源。

这些动作几乎相同,为什么一个起作用而另一个却不起作用?

1 个答案:

答案 0 :(得分:0)

如Igor在评论中所述传递日期时,出现一条错误消息,该错误消息表明我在Permissions区域中有一个Api控制器,该路由的路径也称为api/UserRoutes

更改路线名称后,问题便得以解决。 我只是希望它可以从一开始就告诉我这个错误消息。

相关问题