1
votes

I'm working on a website that has 4 individual sections, each with their own controller. These controllers share a few models, however the model will do things slightly differently based on the controller calling it.

The route that I'd like to follow is {controller}/{model}/{id}/{action} so I could use controller1/cars/5/edit rather than controller1/editcar/5.

Is there any way to have my controller understand a model+action combination and handle them with different ActionResults, or am I forced to use an ActionResult on the model name and then use a conditional to figure out what action to take?

In Ruby I could use get "/controller1/cars/:id/edit" do. I'm just looking for something similar in MVC4.

Example routs:

  • /controller1/cars
    • (returns a view containing all cars from controller1)
  • /controller1/cars/5
    • (returns a view containing car with the ID 5 from controller1)
  • /controller1/cars/5/edit
    • (returns a view to edit car with ID 5 from controller1)
  • /controller2/cars
    • (returns a view containing all cars from controller2)
  • /controller2/boats
    • (returns a view containing all boats from controller2)
2
trying to understand your post. what i dont understand is how a complex object like a model can be part of urlDave Alperovich
It's not necessarily the model that is being a part of the URL, it's something that I could capture at the controller and handle. Most of my MVC experience is with Ruby, so maybe it's not possible with MVC4. I'd just like my controller to take another URL parameter into consideration when determining what method to call.Alec Sanger
it is definitely possible. Do you mind if the parameter is first in the url? is it an exact word or a variable to be used? there subtleties matterDave Alperovich
I dont understand the question but MVC will bind the {model} part of the url to a parameter on the {controller} and {action} method if that helps.Matt Randle
@DaveA, as long as the request is still passed to controller1, that would be fine, the order or the parameters isn't important to me (nor are the values, as I would look for specifics within the controller).Alec Sanger

2 Answers

1
votes

I think this route meets your needs. It requires some clever logic in your action methods but should give you the tools to handle it. Read my description of behavior.

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{model}/{id}/{action}", // URL with parameters
            new { controller = "Home", action = "View", id = UrlParameter.Optional } // Parameter defaults
        );
  • This route will Default to an action called View (that will presumably be used for Display) and has an option to direct to a different Action.
  • Model and id will be passed as arguments to you action method. Unfortunately, Model will not be sent as a type but a string (you may feed that into a Factory class).
  • if if is left out (eg /controller2/boats) it will be passed to your action as a null. This requires logic to handle but gives you the tools to handle it.
0
votes

Thanks for the responses. The reason that I was having so much trouble with this was because I couldn't figure out how to separate controllers with a rout properly. This resulted in my idea breaking away from MVC standards and almost trying to implement a controller of controllers.

When I finally stumbled upon "Areas", I quickly realized that this is what I needed. I was able to add an Area for each section (or area) of my website, and within each area, I could define individual controllers and views, while my models remained in a shared directory outside of all areas. This now works perfectly.