Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

ASP.NET MVC Routing : Dynamic Names for Action Methods

Currently I have a Controller named StoreController. There are three Categories : books, movies, and games. How can i make sure that the url's

match a single action method. Right now, I am having three separate action methods books(); movies(); games(); doing the same thing, i.e listing the products in them

Upvotes: 0

Views: 507

Answers (1)

Mujah Maskey
Mujah Maskey

Reputation: 8804

Did you try like this?

routes.MapRoute(
                "Default", // Route name
                "{controller}/{id}", // URL with parameters
                new { controller = "Store", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                , null }
                )

and you make Controller like

public ActionResult Index(string id)
{
    if(id == "books"){


    }
    else if(id == "movies"){

    }
    else{// this is null case


    }

    return Content("hello");// test
}

Upvotes: 3

Related Questions