views:

39

answers:

1

I want to create an MVC app to generate factsheets, but I'm not sure how to structure the routing and controllers.

It consists of an index page, which acts as a template for the layout of a number of independent panels, each of which contains different types of data.

I want to have a the route template like the following:

/Factsheets/Panels/PanelType?fundId=1&countryId=ABC

so these would be the URLs I'm using:

/Factsheets/Panels/NameAndDatePanel?Afund=1&county=IE  
/Factsheets/Panels/AssetsPanel?fund=1&county=IE

I want a Factsheets controller to be able to supply the Panel controller with the configuration object it needs to generate the type of panel I request.

What should my routing structure look like?

What should my controller structure look like?

edit:

What changes if I want to have a roure structure as follows:

I want to have a the route template like the following:

/Factsheets/ContentArea/Panels/PanelType?fundId=1&countryId=ABC

so these would be the URLs I'm using:

/Factsheets/PageTop/Panels/NameAndDatePanel?Afund=1&county=IE  
/Factsheets/PageTop/Panels/AssetsPanel?fund=1&county=IE

so that FactsheetsController is instantiated to contain the data needed by a ContentArea to know which data to supply to the panel it needs to generate.

+1  A: 

What comes after the ? is the query string so it won't be reflected by the routing engine. So your route might look like this:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{panelType}",
    new { controller = "Factsheets", action = "Panels", panelType = UrlParameter.Optional }
);

And you would have the following controller:

public class FactsheetsController : Controller
{
    public ActionResult Panels(string panelType)
    {
        return View();
    }
}

UPDATE:

If the panelType parameter is not optional you could provide a regex constraint:

routes.MapRoute(
    "Default",
    "Factsheets/Panels/{panelType}",
    new { controller = "Factsheets", action = "Panels", panelType = UrlParameter.Optional },
    new { panelType = ".+" }
);
Darin Dimitrov
what if paneltype isn't optional? I'll update the qeustion to reflect.
DaveDev
Then add a route constraint, see my update.
Darin Dimitrov
Thanks - I've updated the question again.
DaveDev