I have a website defined in Sitecore's site definitions. The path to it is /localhost/mysite/home
. And it works.
I need to create a custom controller to submit forms with an API bypassing Sitecore. So I have FormsController
(inheriting from MVC controller) with an action named "Test" taking no parameters.
I defined the route in the initialize pipeline like this:
public class Initialize
{
public void Process(PipelineArgs args)
{
MapRoutes();
GlassMapperSc.Start();
}
private void MapRoutes()
{
RouteTable.Routes.MapRoute(
"Forms.Test",
"forms/test",
new
{
controller = "FormsController",
action = "Test"
},
new[] { "Forms.Controller.Namespace" });
}
}
The route is added to the route table correctly, and it's there when I debug it. Now, when I try to call method "test", the route is not found and the debugger doesn't hit the breakpoint in the action.
I'm trying different routes:
/localhost/mysite/home/forms/test
/localhost/forms/test
(default website)
But no luck so far.
---- UPDATE ---
Going deeper into it, I noticed that there's something wrong with Sitecore's behavior. The TransferRoutedRequest
processor is supposed to abort the httpRequestBegin
pipeline, giving control back to MVC, in case the context item is null (simplifying). It happens after some checks, among which is one on RoutTable data. But the call to RouteTable.Routes.GetRouteData
returns always null, which makes the processor return without aborting the pipeline. I overrode it to make it abort the pipeline correctly, but still, even if I it calls method args.AbortPipeline()
, pipeline is not aborted and route not resolved.
this is how the original TransferRoutedRequest
looked like:
public class TransferRoutedRequest : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull((object) args, "args");
RouteData routeData = RouteTable.Routes.GetRouteData((HttpContextBase) new HttpContextWrapper(HttpContext.Current));
if (routeData == null)
return;
RouteValueDictionary routeValueDictionary = ObjectExtensions.ValueOrDefault<Route, RouteValueDictionary>(routeData.Route as Route, (Func<Route, RouteValueDictionary>) (r => r.Defaults));
if (routeValueDictionary != null && routeValueDictionary.ContainsKey("scIsFallThrough"))
return;
args.AbortPipeline();
}
}
and this is how I overrode it:
public class TransferRoutedRequest : global::Sitecore.Mvc.Pipelines.HttpRequest.TransferRoutedRequest
{
public override void Process(HttpRequestArgs args)
{
if (Context.Item == null || Context.Item.Visualization.Layout == null)
args.AbortPipeline();
else
base.Process(args);
}
}