I am creating an MVC project using both conventional but since it doesn't work I decided to work on the attribute, at first it was working but after a few runs it gives me 404 so I tried to revert all the code to the first one that was working but unfortunately it gives me 404.
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ReportsFilter",
url: "{controller}/{action}/{filter}/{sdate}/{edate}",
defaults: new { controller = "Reports", action = "GetReports", filter = UrlParameter.Optional, sdate = UrlParameter.Optional, edate = UrlParameter.Optional }
);
routes.MapRoute(
name: "AddOrderProduct",
url: "api/datatable/GetProducts/{OrderId}",
defaults: new { controller = "Orders", action = "GetProducts", OrderId = UrlParameter.Optional }
);
}
OrdersController.cs
[HttpPost]
[Route("api/datatable/OrderGetProduct/{OrderId}")]
public ActionResult GetProducts(int OrderId)
{
db.Configuration.ProxyCreationEnabled = false;
// Server Side Parameters
int start = Convert.ToInt32(Request["start"]);
int length = Convert.ToInt32(Request["length"]);
var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();
var sortColumn = Request.Form.GetValues("order[0][column]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
var orders = db.Order_product.Where(a => a.Order_id == OrderId).Select(b => b.Product_id);
var dtList = db.Products1.Where(w => w.IsActive == true && !orders.Contains(w.Id)).Select(s => new {
s.Name,
Price = s.Base_price,
ProductId = s.Id
}).ToList();
int totalRows = dtList.Count();
// Filter
if (!string.IsNullOrEmpty(searchValue))
{
dtList = dtList.Where(x => x.Name.ToLower().Contains(searchValue.ToLower())
|| x.Price.ToString().Contains(searchValue)
).ToList();
}
int totalRowsFilter = dtList.Count();
// Sorting
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
switch (sortColumn)
{
case "0":
if (sortColumnDir.ToLower() == "asc")
{
dtList = dtList.OrderBy(x => x.Name).ToList();
}
else
{
dtList = dtList.OrderByDescending(x => x.Name).ToList();
}
break;
case "1":
if (sortColumnDir.ToLower() == "asc")
{
dtList = dtList.OrderBy(x => x.Price).ToList();
}
else
{
dtList = dtList.OrderByDescending(x => x.Price).ToList();
}
break;
}
}
// Paging
dtList = dtList.Skip(start).Take(length).ToList();
return Json(new { data = dtList, draw = Request["draw"], recordsTotal = totalRows, recordsFiltered = totalRowsFilter }, JsonRequestBehavior.AllowGet);
}
Running the URL : http://localhost:57848/api/datatable/OrderGetProduct/1
Just gives me error 404. I've tried all solutions posted in here but still getting 404. Please help. Appreciate it new on ASP.NET MVC.
CodePudding user response:
in route attribute, you have to add a forwardslash in the begining of a route, to show that route starts from a root, otherwise the route will be added to the end of a default route for this controller
[HttpPost]
[Route("~/api/datatable/OrderGetProduct/{OrderId}")]
public ActionResult GetProducts(int OrderId)