MVC Routes, Redirection and Sitecore

I know there has been a loooong pause after my last series, past few months have been a crazy head down time doing not so Sitecorish stuff and more on API front.   I like to blog something new I learnt that is totally Sitecore related or affiliated, so, I had to resist the temptation to blog something that is not so uncommon or known by most of us.

Last week I got a kick of something that should be or was assumed to be straight forward, but, just stumped me that I did not know this before. 🙂

In most of the ideal projects of Sitecore, even if they are MVC driven, we do not need to use the custom routes for the most part.  The default one should usually be good enough and if you did have api specific post/get calls that fall through controller that should also have been working fine with out any additional configuration.

So, what if your implementation calls for rendering a specific content item based on URL specific to your needs.  Well, in this case you will see yourself in some trouble if you have patched your Redirection logic to ItemResolver or your customItemResolver for instance.   The problem is the page does not render and correct controllers do not hit, my pages were literally blank leading me no where, I was seeing a ghost in-front of me. lol

It took me some time to figure out what just happened to a page that was working literally before this build, I had to backtrack my steps and realized I fixed a configuration that has now added a new patch to my ItemResolver.  I could have dropped it, but, we all need that Redirect patch don’t we? The marketing and content authors love it and absolutely need it.   So, I had no option but to fix it.

So, to fix this first ensure your order is correct that is –

Item Resolver -> Your Custom Item Resolver -> No sitecore item found -> Redirect Manager Patch -> Still nothing -> Page Not Found Handling

Now, above is typical intuitive flow, but, do understand all projects and baseline could be different and hence you will have to ensure the order is correct according to your application.

My order seems correct, but, still I see the ghost page.  Why ? Why?

So, here is why, if you see closely on your showconfig.aspx, you will see MVC Routing is happening after the above steps noted as we typically end up patching our Redirect logic to ItemResolver leading to issues.  Now, there are two ways you can fix these issues now that you know the cause.

  1. In your custom item resolver, ensure you are configuring and adding code to resolve item from MVC Routes.  It should be pretty straight forward and there should be bunch of examples I am sure to get this done with lot of duplication of code based on how many routes you have actually got going on your RouteConfig.cs
  2. You could also save your self some copy paste effort(I usually make tons of mistakes when I do copy paste, it makes me lazy. 😉 ) and patch your Redirect logic to right after MVC pipelines.  Below is a screenshot of how that may look like –

Order_redirect

Hopefully it will save you some time thinking why your custom routes are not working when redirect is used along side of ItemResolver.  Happy Sitecoring people!

I promise to blog back pretty soon this time and I am super excited to meet all my favorite people in Orlando, Florida for the symposium.  This is my first symposium attendance and being an MVP this year as well is a total icing on the experience. 🙂