1

I have a simple Sitecore controller that returns an list of items from Sitecore. It's used it in a custom SPEAK UI page.

This works fine in my local dev environment but when I deploy it into an Sitecore environment (which has Castle Windsor setup as an IoC) my controller no longer works. The error in the logs is: 'Could not create controller':

public class MyListBrowsingController : Controller
{
    public JsonResult GetItems()
    {
        var myList = //get list here
        return Json(myList, JsonRequestBehavior.AllowGet);
    }

}

As I understand it the problem is that CastleWindsor is wired up via a pipeline and is overwritten the default Sitecore Dependency Resolver, e.g like so:

https://bitlush.com/blog/using-castle-windsor-with-sitecore-mvc-for-dependency-injection

I know ideally this would be solved with a fall-back like so: https://github.com/HedgehogDevelopment/sitecore-chained-dependency-resolver/blob/master/InversionOfControl.CastleWindsor/Pipelines/SetupDependencyResolvers.cs.

However unfortunately I don't have access / control of this code as it's in a separate dll so id like to explicitly register the route in some other way which won't get overwritten by an IoC.

Is there a way to do this?

Further info:

  • Sitecore 7.5 > 8.1
  • 1
    pre 8.2 DI? or post? – Mark Cassidy Feb 28 '17 at 15:25
  • Sorry Mark I should add this is for 7.5 > 8.1 only currently. I may need to find a solution for 8.2 also though. – Adam Seabridge Feb 28 '17 at 17:27
  • Can you register it with the current IoC, or do you not have access to it? – Chris Auer Feb 28 '17 at 17:46
  • No I don't have access to It. Thats the problem. Else I'd just add it to the container I guess. I need a solution where it ignores / overrides the IoC I think. – Adam Seabridge Feb 28 '17 at 17:53
  • @MarkCassidy Did you have some ideas on how to go about doing this? – Adam Seabridge Mar 1 '17 at 10:55
1

For anyone coming across this here is what I did.

If you have a DI configured it resets any routes registered after you've added them so you need to register them in the initialize Pipeline like so:

Create a pipeline to register your route:

public class RegisterMySampleRoute
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapHttpRoute("my_sample_route", 
            "mysample/api/{controller}/{action}", 
            new {controller = "SampleController"}
        );
    }
}

Then patch it in like so:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <initialize>
                <processor type="MyNamespace.Pipelines.RegisterMySampleRoute, MyNamespace" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
            </initialize>
        </pipelines>
    </sitecore>
</configuration>

You should then find your custom routes will work as expected.

|improve this answer|||||

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.