Я пытаюсь поддерживать как веб-API, так и обычные контроллеры.
Я определил разные методы для разных действий, но похоже, что что-то не так, так как я снова и снова получаю следующую ошибку
{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request:
\r\nPostFilter on type imaserver.Controllers.OrdersController\r\nPostOrder on type imaserver.Controllers.OrdersController",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":"
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n
at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n
at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
Вот мой код:
RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"CDN",
"cdn/{directory}/{file}",
new { controller = "CDN", action = "Download", directory = UrlParameter.Optional, file = UrlParameter.Optional }
);
routes.MapRoute(
name: "API",
url: "api/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
}
}
OrderController
public class OrdersController : ApiController
{
private FoodDeliveryEntities db = new FoodDeliveryEntities();
// GET: api/Orders
public IQueryable<Order> GetOrders()
{
db.Configuration.ProxyCreationEnabled = false;
return db.Orders;
}
// GET: api/Orders/5
[ResponseType(typeof(Order))]
public IHttpActionResult GetOrder(long id)
{
db.Configuration.ProxyCreationEnabled = false;
Order order = db.Orders.Find(id);
if (order == null)
{
return NotFound();
}
return Ok(order);
}
// PUT: api/Orders/5
[ResponseType(typeof(void))]
public IHttpActionResult PutOrder(long id, Order order)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != order.Id)
{
return BadRequest();
}
db.Entry(order).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!OrderExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Orders/filter
[HttpPost]
public IHttpActionResult PostFilter(OrderFilter orderFilter)
{
db.Configuration.ProxyCreationEnabled = false;
if (orderFilter.OrderType != null)
return Ok(db.Orders.Include(o => o.OrderItems.Select(i => i.Item)).Include(c => c.Customer).OrderByDescending(o => o.PickupDate).Where(o => o.OrderType.Equals(orderFilter.OrderType)));
else if (orderFilter.Status != null)
return Ok(db.Orders.Include(o => o.OrderItems.Select(i => i.Item)).Include(c => c.Customer).OrderByDescending(o => o.PickupDate).Where(o => o.Status.Equals(orderFilter.Status)));
else
return Ok(db.Orders.Include(o => o.OrderItems.Select(i => i.Item)).Include(c => c.Customer));
}
// POST: api/Orders
[ResponseType(typeof(Order))]
public IHttpActionResult PostOrder(Order order)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Configuration.ProxyCreationEnabled = false;
order.Status = "New";
order.CreatedAt = DateTime.Now;
order.UpdatedAt = DateTime.Now;
db.Orders.Add(order);
db.SaveChanges();
foreach (var orderItem in order.OrderItems)
{
orderItem.OrderId = order.Id;
orderItem.CreatedAt = DateTime.Now;
orderItem.UpdatedAt = DateTime.Now;
orderItem.IsActive = true;
}
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = order.Id }, order);
}
// DELETE: api/Orders/5
[ResponseType(typeof(Order))]
public IHttpActionResult DeleteOrder(long id)
{
Order order = db.Orders.Find(id);
if (order == null)
{
return NotFound();
}
db.Orders.Remove(order);
db.SaveChanges();
return Ok(order);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool OrderExists(long id)
{
return db.Orders.Count(e => e.Id == id) > 0;
}
public partial class OrderFilter
{
public string Status { get; set; }
public string OrderType { get; set; }
}
}
Я также попытался переименовать PostFilter в Filter, но бесполезен
Я запускаю этот URL:
http://somewebsite/api/Orders/filter
POST
Что я делаю не так?