DragonLady
DragonLady

Reputation: 41

MVC3 how to minimize the number of controllers

i am new to MVC3 and our project needs something like:

http://www.abc.com/product_1/product_1_subpage/... http://www.abc.com/product_2/product_2_subpage/...

right now, i have product_1 as a controller; product_1_subpage as an action of this controller, however, think about i have over 100 different products, i cannot keep create over 100 controllers for each single product, i need to do something on this structure, any idea?

thank you very much for your help, really appreciate any input.

Upvotes: 3

Views: 119

Answers (3)

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

You would probably want to have only a single Controller called Products, for all your products, instead of having to add a new controller for every product.

With custom routes, or the default routing for that matter, you would still be able to generate individual links for individual products. Also, if you were to use your approach with a new controller for every product (which you really shouldn't!), you would have to re-compile and deploy your application every time you want to add another product - which would be a pain to maintain.

It sounds like you should have a look at the tutorials about MVC provided by the .Net team to get some basic understand of MVC, and how to think about it.

Upvotes: 2

jgauffin
jgauffin

Reputation: 101166

Use custom routes:

routes.MapRoute(
            "ProductsRoute", // Route name
            "products/{productName}/{subName}/{id}", // URL with parameters
            new { controller = "Product", action = "View", id = UrlParameter.Optional } // Parameter defaults
        );

That would make the following work:

public class ProductController : Controller
{
    // http://yourweb/products/goggles/xray/Elite2000
    public ActionResult View(string productName, string subName, string id)
    {
    }
}

Upvotes: 1

javram
javram

Reputation: 2645

how about changing the format of the url a little to take advantage of the routing:

http://www.abc.com/product/subpage/1

Upvotes: 0

Related Questions