Update:
As someone pointed out I was missing an s in my Route registration. Now I have a secondary problem.
This is how I want it to work:
http://localhost/products/ --> ProductsController.Index()
http://localhost/products/3/apples --> ProductsController.Details(int? id, string productName)
This is what currently happens:
http://localhost/products goes to my Details action.
How do I set up the routes for this?
I set up the following routes in my Global.asx file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ViewProduct",
"products/{id}/{productName}",
new { controller = "Products", action = "Details", id = 0, productName = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I have the following controller:
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult Index()
{
IList<Product> products = new List<Product>
{
new Product { Id = 57349, ProductName = "Apple" },
new Product { Id = 57350, ProductName = "Banana" }
};
return View(products);
}
public ActionResult Details(int? id, string productName)
{
Product p = new Product { Id = id.Value, ProductName = productName };
return View(p);
}
}