DD4T Default route not working
Asked Answered
T

5

5

I am using the DD4T framework with SDL Tridion 2011 and ASP.NET MVC4

I have some non-DD4T Controllers and Views which I want to use, however I am getting a 404 error when I try to go to these urls.

This is my route table

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Route for SDL Tridion UI 2011 (Aka !SiteEdit)
        routes.MapRoute(
            "SiteEditBlankPage",
            "se_blank.html",
            new { controller = "Empty", action = "Index" });

        routes.MapRoute(
            null, // Route name
            "xml/{*PageId}", // URL with parameters
            new { controller = "Page", action = "Xml" }, // Parameter defaults
           new { pageId = @"^(.*)?$" } // Parameter constraints
        );

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

        routes.MapRoute(
            null, // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

I have tried moving the default route above the Tridion one but then I get 404s for the Tridion pages.

The only way to get it working it seems is to have a specific route to my controller e.g:

        routes.MapRoute(
           null, // Route name
           "MyController/{action}/{id}", // URL with parameters
           new { controller = "MyController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
       );

Anyone have any ideas? As the above solution is not ideal.

(I do not have any UI 2012 config in my web.config)

Titillate answered 31/10, 2012 at 12:40 Comment(1)
What do you mean by 'normal controllers'? Non-dd4t controllers, I guess?Schwann
C
7

There are several options AFAICS:

  1. If you can recognize a Tridion url from a 'normal' url (extension, startpath, etc) you can solve it by adjusting the TridionPage route. But this is no option for you I think, because if you can recognize the Tridion url's from the normal one's you already would have come up with this solution ;)

  2. Implement a route-constraint on the TridionPage route. Check in this routeconstraint if the requested url is in the Broker. If true: return true, otherwise false. If it returns false, the next route (that matches) will handle the request. Not sure about performance though.

  3. Your own option: a specific route for your normal pages.

I am sure I missed some options. Hopefully someone else shares them here.

Coracle answered 31/10, 2012 at 14:2 Comment(2)
Thanks Albert. I think I'm probably going to have to go with the 3rd solution for now! If I think of a better way, I will update here.Titillate
+1 to the specific route - that's how we tackled this. Obviously you dont have to have "MyController" in the url!Herold
S
6

Normal DD4T operation is to let Tridion own the URLs, i.e. catch all URLs and pass them to one PageController. This interferes with the automatic routing of controllers/actions based on their names, so one of these niceties has to give!

I personally like Albert's first suggestion because it is simple. E.g. map only URLs ending with '.html' to Tridion. Unfortunately, this causes a problem with default pages in SGs, since "/sg" is not recognized, even though you may have a "/sg/index.html" in the broker. Some alternatives to work around that:

  • Configure explicit routes for all your first-level structure groups (/news, /products, etc)

  • Publish all Tridion pages into a subsection of your site, e.g. /content and map that to a specific route

Schwann answered 31/10, 2012 at 14:27 Comment(1)
Thanks Quirijn. I'm not sure I can tell a Tridion Page from a "normal" action so I'm going to go with creating routes for my controllers I think.Titillate
T
3

We just placed a page se_blank.html in the root and made sure it was only deployed to staging so it was there where needed and didn't require a specific route.

That is not really answering your question of course, but you can consider it as an option perhaps.

Thrasonical answered 31/10, 2012 at 13:26 Comment(0)
A
3

Perhaps you could make a catch all mapping like the TridionPage mapping to a Controller which redirects to the correct controller and action based on the parameters with the RedirectToAction function?

Not sure if this is an ideal solution for you.

Anthe answered 31/10, 2012 at 13:37 Comment(0)
D
0

Our solution: Tridion doesn't allow to create pages with 'mvc' inside the link => we defined a constraint that ignore PageId with 'mvc' in RouteConfig.

So: when DD4T receives a request for a "tridion" page, it uses the TridionPage route and when it's an request /Controller/Action it uses default MVC route.

routes.MapRoute(
"TridionPage",
"{PageId}",
new { controller = "Page", action = "Page" }, //parameter defaults
new { PageId = @"(?!mvc.)*" } // Parameter constraint: ignore PageId with '/'
);

//Default MVC route
routes.MapRoute(
"MVC", //Route name
"mvc/{controller}/{action}/{id}", 
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
Delenadeleon answered 3/10, 2013 at 8:16 Comment(1)
What about pages in a structure group? Surely they would contain a slash?Titillate

© 2022 - 2024 — McMap. All rights reserved.