4

I have a multi-site Sitecore instance with several sites defined using the physicalFolder/virtualFolder attributes defined on the <site> nodes all done with MVC. There is some functionality I would like to add to a page in a more native-MVC fashion where I can invoke the controller thru Javascript and update the DOM elements on the response with the updated html from the controller (ie: /api/sitecore/search/results). The issue I am seeing, since I am inoking the controller directly/natively, it's a bit outside the Sitecore execution pipeline so I have no handle on the Sitecore.Context.Site. Ultimately, what I would like is to do make the routing respect the virtual folder so I can at least get a handle on the SiteContext (ie: /site1/api/sitecore/search/results).

7

What I ended up doing is registering a single route on pipelines/initialize:

<pipelines>
    <initialize>
        <processor type="MyFramework.Routing.RegisterVirtualFoldersRoutes, MyFramework" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
    </initialize>
</pipelines>

and in the pipeline itself:

namespace MyFramework.Routing
{
    public class RegisterVirtualFoldersRoutes
    {
        public virtual void Process(PipelineArgs args)
        {
            RouteTable.Routes.MapRoute("VirtualFolderRouting", "{virtualFolder}/api/sitecore/{controller}/{action}/{id}", new { id = UrlParameter.Optional });
        }
    }
}

Without any additional customization this allowed me to invoke /site1/api/sitecore/search/results or /site2/api/sitecore/search/results directly and get the SiteContext of the site respective of the virtualFolder. I assume there may be some collisions between the virtualfolder/api/sitecore and other defined routes, but none of them come to mind currently.

  • This perfectly worked for my virtual paths which contain no forward slashes. thanks – Alireza Jan 10 at 12:29
  • In the meantime, I'm trying to find a way to make it work with site containing slashes in their virtual folder. Do you have any idea? – Alireza Jan 10 at 12:30
  • @Alireza, could you provide an example of what kind of virtual folder you would ultimately be using? – vandsh Jan 10 at 17:20
  • 0 Some of my websites contain one or more "/" in their virtual folders, something like /demo/v1, /demo/v2, dev/v1, dev/v2, etc. Due to some marketing and design restrictions, I have to put them in separate websites. I'm doing something now, and I'll share it here with you when it's done :) – Alireza Jan 11 at 14:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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