Irfan Y
Irfan Y

Reputation: 1310

how to create hierarchy of pages in asp.net MVC

I am working on a classified website that will have links like "electronics/mobiles/samsung/samsungS3/adTitle". How to create hierarchy of views like that in asp.net. If the answer is HMVC then please refer some link that contains complete guide how to implement HMVC.

Upvotes: 1

Views: 787

Answers (2)

Haseeb Asif
Haseeb Asif

Reputation: 1786

You don't need to create views in this hierarchy but you need to create URLs in this fashion and that is called friendly URLs.

Look at following stack overflow question How can I create a friendly URL in ASP.NET MVC? and Friendly URL


You will be defining another route which will end up on your single action method. So You will add a route in routeConfig.cs as follows

 routes.MapRoute(
                name: "custom",
                url: "{category}/{type}/{manufacturer}/{version}/{Title}",
                defaults: new { controller = "Home", action = "customRoute"}
            );

and your custom action will have all values passed in as param be as follows

public string customRoute(string category, string type, string manufacturer, string version, string Title)
{
    return category + type + manufacturer + version + Title;
}

You can achieve the same using action based routing as well

 // eg: electronics/mobiles/samsung/samsungS3/adTitle
    [Route("{category}/{type}/{manufacturer}/{Title}")]
    public ActionResult Index(string cateogry, string type,string manfacture, string Title) { ... }

Upvotes: 1

teo van kot
teo van kot

Reputation: 12491

You don't need hierarchy of Views, you should use Route Config that will allow you to get View that you need base on URL.

From 4 Version of MVC also have areas not only controlles and View by default. So check this tutorial to know how to customise your Routing.

Upvotes: 3

Related Questions