I am trying to expose a MVC route in Sitecore 9.2 for a web api I am creating.
I followed the steps describe here but it only works when my controller inherits from controller. If I inherit it to ServicesApiController I receive the following error.
The controller for path '/api/messages' was not found or does not implement IController.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Sitecore.Mvc.Diagnostics.ExceptionWrapper: The controller for path '/api/messages' was not found or does not implement IController.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ExceptionWrapper: The controller for path '/api/messages' was not found or does not implement IController.]
[ControllerCreationException: Could not create controller: 'Messages'.
The current route url is: 'api/messages'. ]
Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName) +171
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +261
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1128
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +220
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +134
UPDATE I checked the Sitecore.Mvc.Controllers.SitecoreControllerFactory and notice that inside the method ResolveController factory has a cast to IController.
protected virtual IController ResolveController(Type type)
{
return (DependencyResolver().GetService(type) as IController) ?? TypeHelper.CreateObject<IController>(type, Array.Empty<object>());
}
protected virtual IController CreateSitecoreController(RequestContext requestContext, string controllerName)
{
IController controller = PipelineService.Get().RunPipeline("mvc.createController", new CreateControllerArgs(requestContext, controllerName), (CreateControllerArgs args) => args.Result);
if (controller == null)
{
throw new InvalidOperationException("Could not create a Sitecore controller: '{0}'".FormatWith(controllerName));
}
return controller;
}
Does this mean that you can no longer inherit from ServiceApiController in Sitecore 9.2 like what I am trying to do since the ServiceApiController inherits from APIController and not on Controller or IController?
It would be helpful if you guys can confirm as well.
UPDATE 2
I tried inheriting from the IController, the error disappears but the action is not called or executed.
Below are my codes:
Config:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="SAIF.Foundation.Bot.Pipeline.BotLoadProcessor,SAIF.Foundation.Bot" patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']" />
</initialize>
</pipelines>
</sitecore>
</configuration>
RouteConfig.cs
namespace SAIF.Foundation.Bot
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("botroutes", "api/messages", new { controller= "Messages", action="index" });
}
}
}
BotLoaderProcessor.cs
namespace SAIF.Foundation.Bot.Pipeline
{
using Sitecore.Diagnostics;
using Sitecore.Pipelines;
using System.Web.Routing;
public class BotLoadProcessor
{
public void Process(PipelineArgs args)
{
Log.Info("Sitecore is starting", this);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
MessagesController.cs
public class MessagesController : ServicesApiController
{
// GET: Bot
private readonly IBotFrameworkHttpAdapter Adapter;
private readonly IBot Bot;
public MessagesController()
{
Adapter = new BotFrameworkHttpAdapter();
Bot = new SimpleBot();
}
[System.Web.Http.HttpPost]
[AcceptVerbs("Post")]
public async Task<HttpResponseMessage> Index()
{
var response = new HttpResponseMessage();
await Adapter.ProcessAsync(Request, response, Bot);
return response;
}
}