0

I am building a C# application and everything seems to be right, but I am experiencing following error:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Products/Main

Everything seems to be correct. I have products model:

public class ProductsController : Controller
{
        // GET: Product
        public ActionResult Main(string category,
                                  string name,
                                  SortCriteria? criteria,
                                  SortOrder? order)
        {
            var model = new ProductsViewModel
            {
                Category = category,
                Name = name,
                Criteria = criteria ?? SortCriteria.Name,
                Order = order ?? SortOrder.ASC
            };

            var products = new ProductRepository().GetAll();
            model.Categories =
                products.Select(p => p.Category).Distinct().Select(c => new SelectListItem { Value = c, Text = c }).ToList();

            if (!string.IsNullOrEmpty(category))
            {
                products = products.Where(p => p.Category.Equals(category));
            }

            if (!string.IsNullOrEmpty(name))
            {
                products = products.Where(p => p.Name.ToLower().Contains(name.ToLower()));
            }

            if (criteria == SortCriteria.Name)
            {
                if (order == SortOrder.DESC)
                    products = products.OrderByDescending(p => p.Name);
                else
                    products = products.OrderBy(p => p.Name);
            }
            else
            {
                if (order == SortOrder.DESC)
                    products = products.OrderByDescending(p => p.Price);
                else
                    products = products.OrderBy(p => p.Price);
            }

            model.Products = products.Select(MapToModel).ToList();

            return View(model);
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View(new ProductViewModel());
        }

        [HttpPost]
        public ActionResult Create(ProductViewModel model)
        {
            new ProductRepository().Create(MapFromModel(model));
            return RedirectToAction("Index");
        }

        private ProductViewModel MapToModel(Product p)
        {
            return new ProductViewModel
            {
                Id = p.ID,
                Name = p.Name,
                Price = p.Price,
                Category = p.Category
            };
        }

        private Product MapFromModel(ProductViewModel p)
        {
            return new Product
            {
                ID = p.Id,
                Name = p.Name,
                Price = p.Price
            };
        }
}

I have a View for this main action result.

And here is my URL format:

routes.MapRoute(
            name: "Default",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new {culture="en", controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
Community
  • 1
  • 1

1 Answers1

-2

Enter controller name and action method name otherwise change controller and action method name in route.config file...

routes.MapRoute(
            name: "Default",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new {culture="en", controller = "Products", action = "Main", id = UrlParameter.Optional }
        );
Chamath
  • 2,016
  • 2
  • 21
  • 30