14

So it looks like there are at least a couple people out there who have used DD4T with MVC5. Has anyone used it with MVC 5 Areas though? What were the issues? It seems like the additional area routes may be an issue. I am about to see how big of a concern this is, but if someone else has already gone through the pain I would like to learn from that.

This is not as straight forward as I think people are assuming and is related to DD4T unless I am missing something.

More details: DD4T defines this route

// Tridion page route
routes.MapRoute(
"TridionPage",
"{*PageId}",
new { controller = "Page", action = "Page" }, // Parameter defaults
new { pageId = @"^(.*)?$" }); // Parameter constraints

and you have this type of folder structure for it.

enter image description here

All is good. Now you want to add an area. So you use the VS scaffolding to say add area. You now have this folder structure.

enter image description here

I also register a new route.

 // Tridion page route
        context.MapRoute(
           "TridionPageTest",
           "Test/{*PageId}",
           new { controller = "Page", action = "Page" }, // Parameter defaults
           new { pageId = @"^(.*)?$" }, 
           new[] { "<namespace>.Web.Controllers.Test" }); // Parameter constraints

Notice that I add a namespace to my route to separate my area controllers from my default controllers.

Run the code by going to http:///Test and the PageController in my Test area is hit. So all is good.

The issue is when the base.Page(pageId) method is called it returns index.aspx page from the Broker. This has a view on it of "Default" which DD4T then load the Default.cshtml view from the root and not the area. Then the default view tries to load and does but calles

@Html.RenderComponentPresentations()

IN DD4T code calls DefaultComponentPresentationRender.RenderComponentPresentation method there is this call. controller = ConfigurationHelper.ComponentPresentationController.

Which under the covers in DD4T is always mapped to the one ComponentController in the config file. So since DD4T is forcing the use of some controllers I am not sure it is as simple as " there would be no relationship between DD4T and your MVC Areas; they would be completely independent."

  • 1
    How do you plan to make use of areas? Generally speaking there should be no issues as they will be routed independently of your DD4T controllers. Also worth noting that areas have been around (and fundamentally the same) since MVC2. – Ant P Jun 17 '14 at 22:07
  • These would be the Areas found under Visual Studio's Add Scaffold dialog and roughly represent website "sections" or maybe types of pages. @AntP, any thoughts on a relationship to Tridion Structure Groups or Page Types for MVC areas, perhaps? Or would the use of areas be up to development? – Alvin Reyes Jun 18 '14 at 0:58
  • I have different partner groups and want to have an area for each one to help keep viewers organized (I will have close to 100 views). So far in trying setting up a area route to a new page controller and clarifying by namespace works. However the RenderComponentPresentaion call fails say it can't find an instance and the path is not to the area view. – ToddB Jun 18 '14 at 1:04
  • Just to clarify a little. This is the error I get. "No route in the route table matches the supplied values." trying to hit "\Views\Page\Default.cshtml". The right controller is hit but the wrong page path are there is no "area" in front of the page. – ToddB Jun 18 '14 at 1:21
  • I think I found the issue. In the DefaultComponentPresentationRender.RenderComponentPresentation method there is this call. controller = ConfigurationHelper.ComponentPresentationController. This gets the CompnentPresentationController name from the config file which can only be one and is the default one in the default MVC path. Still trying to verify. – ToddB Jun 18 '14 at 1:48
11

ok I think I figured out the part I was missing. I assumed... wrongly... that the default route was not being used but it is. That is the route the ComponentController goes through. In my area route config I was not defining the default route, hence it was complaining about the route not being found.

Once I added the following to my area route config both the default controllers and the area controllers, and their correct cshtml pages, are being loaded.

// Tridion page route
        context.MapRoute(
           "<mynewdefaultroutename>",
           "<area>/{*PageId}",
           new { controller = "Page", action = "Page" }, // Parameter defaults
           new { pageId = @"^(.*)?$" }, 
           new[] { "<namespace of my area controller>" }); // Parameter constraints    

        context.MapRoute(
            name: "<mynewdefaultroutename>",
            url: "<new area>/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "<namespace of my area controller>" });

Thanks for the help guys. It looks like DD4T is able to handle this route just fine so long as you setup the right routes. Just don't forget that the both routes are needed in your area route config!

| improve this answer | |
  • 2
    Indeed - child actions effectively follow the same pipeline as ordinary actions, meaning that, as a side-effect, you have to have a valid route to match your action. The red herring was the fact that DD4T was using ComponentController from your config but that controller name was still being resolved in the context of your area, which is the very reason it wasn't working. – Ant P Jun 18 '14 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.