0

大家好,我的控制器 api 中有 2 个方法:

[HttpPost]
[HttpGet]
public IEnumerable<Hotel> Get(HotelSearch hotelSearch)
{
    try
    {
        if (hotelSearch == null)
        {
            hotelSearch = new HotelSearch
            {
                Rooms = new List<RoomSearch> { new RoomSearch { AdultsQuantity = 1, ChildrenQuantity = 0 } },
                Stars = 0,
                City = "MIA",
                IsoCountry = "US",
                DepartureDate = Convert.ToDateTime("10/10/2013"),
                ArrivalDate = Convert.ToDateTime("17/10/2013")
            };
        }
    }
    catch (Exception ex)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
    }

    return HotelService.GetHotel(hotelSearch);
}

[HttpPost]
[HttpGet]
public Hotel GetDetails(Hotel hotel)
{
    //return HotelService.GetHotelDetails(hotel);
    return new Hotel();
}

关注我的 WebApiConfig:

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

当我尝试从 /api/Hotel/GetDetails/ 访问某些方法时,返回一条消息:“找到与请求匹配的多个操作”。

谢谢并恭祝安康。

4

2 回答 2

4

您应该对 [HttpPost] 和 [HttpGet] 使用单独的方法

于 2013-09-04T18:26:05.163 回答
0

您应该添加自定义路线,如下所示

public static void Register(HttpConfiguration config)
        {
             config.Routes.MapHttpRoute(
              name: "ApiByName",
              routeTemplate: "api/{controller}/{action}/{name}",
              defaults: null,
              constraints: new { name = @"^[a-z]+$" }
             );
            config.Routes.MapHttpRoute(
                name: "ApiByAction",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { action = "Get" }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

        }

希望这可以帮助

于 2013-09-04T18:24:34.090 回答