vendredi 27 février 2015

Custom MVC Routes


Vote count:

0




So I just got to routes on my first big MVC app and its giving me a headache.


All /Category/Subcategory goes to the ProductController.Index(string category, string subcategory). If I don't specify a Subcategory it will show me all items in the Category (Fords, Audis etc)


If I have my routes like this, url /Home/About goes to Product/Index with parameters: Category=Home, Subcategory=About.


If I move the default to the top, url /Car/Ford does not go anywhere since I don't have a CarController (I don't want a controller for every category.)


I'd really like to not have another segment like /store/Car/Ford..


Do I need to create a route for every "main" Category and hardcode the name? Help me solve this problem!



// http://localhost/Car/Ford (note: Car can be replaced by bike, plan, boat etc.)
routes.MapRoute(
name: "AllProductsInCategoryOrSubcategory",
url: "{category}/{subcategory}",
defaults: new { controller = "Products", action = "Index", subcategory = UrlParameter.Optional }
);

// http://localhost/Car/Ford/ABC123/Explorer
// http://localhost/Car/Ford/ABC123
routes.MapRoute(
name: "Kakel",
url: "{category}/{subcategory}/{id}/{productName}",
defaults: new { controller = "Products", action = "Details", produktNamn = UrlParameter.Optional }
);

// http://localhost/Home/About
// http://localhost/Products/RenderImage/ABC123 (used to render image in details view)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);


asked 53 secs ago

Rob

40






Custom MVC Routes

Aucun commentaire:

Enregistrer un commentaire