1

i've created a basic mvc3 website whereby each controller represents the first folder in a url structure.

for example, the "food" and "drinks" folders below are controller. there are only two controllers which contain all of the sub-items in them.

ie in the first line of the example, controller=food, method=asian

in the second line controller=food, method=pad-thai and so on and so forth.

www.mysite.com/food/asian/ www.mysite.com/food/asian/pad-thai www.mysite.com/food/italian/chicken-parmigiana www.mysite.com/drinks/cocktails/bloody-mary

how would i write routes so that www.mysite.com/food/asian/pad-thai will direct to the food controller and the paid thai method within that controller, and also have a rule to send from www.mysite.com/food/asian/ to the food controller and asian index method??

Dkong
  • 2,748
  • 10
  • 54
  • 73

1 Answers1

3

The MVC design pattern isn't for rewriting URLs to point to folder structures. It can do this but it certainly isn't its main purpose. If you're trying to create a URL structure with static content, it might be easier to use the URL rewriting functionality built into IIS.

If you're creating a full MVC application, set up FoodController and DrinkController to serve up your views, for example:

public class FoodController : Controller
{
  public ActionResult ViewDishByTag(string itemType, string itemTag)
  {

    // If an itemType is displayed without itemTag, return an 'index' list of possible dishes...

    // Alternatively, either return a "static" view of your page, e.g.
    if (itemTag== "pad-thai") 
         return View("PadThai"); // where PadThai is a view in your shared views folder

     // Alternatively, look up the dish information in a database and bind return it to the view
     return ("RecipeView", myRepo.GetDishByTag(itemTag));
  }
}

Using the example above, your route might look a little like this:

routes.MapRoute(
                "myRoute",
                "{controller}/{itemType}/{itemTag}",
                new
                {
                    controller = UrlParameter.Required,
                    action = "ViewDishByTag",
                    itemtype = UrlParameter.Optional,
                    itemTag = UrlParameter.Optional
                }
            );

Your question doesn't contain much detail about your implementation, so if you'd like me to expand on anything, please update your question.

Community
  • 1
  • 1
Nick
  • 5,844
  • 11
  • 52
  • 98