Update:
As someone remarked, I didnโt have the registration of my route. 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 is currently happening:
http: // localhost / products goes to my verbose action.
How to set up routes for this?
I installed the following routes in the 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); } }