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, I think I have over 100 different products, I cannot create more than 100 controllers for each individual product, I need to do something in this structure, any idea?

Thank you so much for your help, really appreciate any input.

+3


source to share


3 answers


You probably want to have only one controller called Products for all of your products, rather than adding a new controller for each product.

When using custom routes or default routing, you can still create separate links for individual products. Also, if you were to take your approach with a new controller for each product (which you really shouldn't!), You would have to re-compile and deploy your application every time you want to add a different product, which would be a pain to maintain.



It looks like you should take a look at the MVC tutorials provided by the .Net team to get a basic understanding of MVC and how to think about it.

+2


source


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
        );

      



This will do the following job:

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

      

+1


source


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

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

      

0


source







All Articles