Web API一个动作是有效的,而几乎相同的动作没有?

问题描述 投票:0回答:1

错误信息

{
    "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调用,所以我无法想象它将成为问题的根源。

这些行为几乎相同,为什么一个工作而另一个不工作?

asp.net-web-api2 asp.net-web-api-routing
1个回答
0
投票

正如伊戈尔在评论中所说的那样传递日期时,出现了一条错误消息,显示我在我的Permissions区域有一个Api控制器,其路线也名为api/UserRoutes

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

© www.soinside.com 2019 - 2024. All rights reserved.