I wondered if anyone can tell me where I am going wrong. I am trying to setup a custom route to a non umbraco controller, however it never hits the controller and results in a 404 error.
My route is registered as below
public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
{
public override void Compose(Composition composition)
{
//You can do this for a single controller - Lifetime is MEGA important
composition.Register(typeof(Vantage.Controllers.BusinessController), Lifetime.Request);
}
}
public class RegisterCustomRouteComponent : IComponent
{
private readonly IUmbracoContextFactory _context;
public RegisterCustomRouteComponent(IUmbracoContextFactory context)
{
_context = context;
}
public void Initialize()
{
using (var cref = _context.EnsureUmbracoContext())
{
var umbracoHelper = cref.UmbracoContext.Content;
var root = umbracoHelper
.GetAtRoot()
.DescendantsOrSelfOfType("home")
.FirstOrDefault();
RouteTable.Routes.MapRoute(
"BusinessDetails",
"Business/{action}/{id}",
new
{
controller = "Business",
action = "Details",
id = UrlParameter.Optional
});
}
}
public void Terminate()
{
// Nothing to terminate
}
}
My Controller looks like
public class BusinessController : Controller
{
public ActionResult Details(string id)
{
var business = InstallerSearchRepo.GetInstaller(id);
var techdetails = CrmUtils.GetInstallerDetails(id);
... some processing logic
return View("~/Views/BusinessDetails.cshtml", business );
}
}
typing a url like /Business/Details/xxx-xxxx-xxx-xxxx just results in a page not found and never hits the controller action.
When you register a Component with Umbraco's collection of Components, you can either 'add it to the collection' inside a composer:
public class SubscribeToContentServiceSavingComposer : IUserComposer
{
public void Compose(Composition composition)
{ composition.Components().Append<SubscribeToContentServiceSavingComponent>();
}
}
or you can make use of ComponentComposer
public class SubscribeToContentServiceSavingComposer : ComponentComposer<SubscribeToContentServiceSavingComponent>
I think the problem here is you've taken this second approach to try to register the component - but then chosen to 'override' the 'Compose' step to also register your Controller.... but by overriding, you remove the base implementation of Compose, which is where your Component 'would' have been added to the list of Components!
So either manually add it back in, or perhaps call the underlying base method of Compose first to make sure the component gets added.
Also on another note if your Business Details page will contain content from Umbraco or share implementation views (eg header and footer that contains Umbraco content) then consider mapping an UmbracoRoute with a VirtualNodeRouteHandler to provide UmbracoContext to your custom content.
Hi Marc,
Thanks for the response, my problem was that if I didn't override the compose method to register my controller then I get a different error about it not being registered, don't remember exactly as now doing it the ugly way by passing in a query string parameter due to time constraints, overriding the compose method removed the error but nothing got to the controller.
It would appear it does not get mapped as it never hits the controller method.
I don't need access to Umbraco content, I simply want to pass in an integer which the action uses to pull data from an external source.
I currently have it working using a surfacecontroller action and passing a ?id=XXXX in the query string, but was trying to create a route so that it could be action/id without the need for the query string
Custom route not hitting controller action
I wondered if anyone can tell me where I am going wrong. I am trying to setup a custom route to a non umbraco controller, however it never hits the controller and results in a 404 error.
My route is registered as below
My Controller looks like
typing a url like /Business/Details/xxx-xxxx-xxx-xxxx just results in a page not found and never hits the controller action.
Hi Huw
When you register a Component with Umbraco's collection of Components, you can either 'add it to the collection' inside a composer:
or you can make use of ComponentComposer
I think the problem here is you've taken this second approach to try to register the component - but then chosen to 'override' the 'Compose' step to also register your Controller.... but by overriding, you remove the base implementation of Compose, which is where your Component 'would' have been added to the list of Components!
So either manually add it back in, or perhaps call the underlying base method of Compose first to make sure the component gets added.
Also on another note if your Business Details page will contain content from Umbraco or share implementation views (eg header and footer that contains Umbraco content) then consider mapping an UmbracoRoute with a VirtualNodeRouteHandler to provide UmbracoContext to your custom content.
https://our.umbraco.com/Documentation/Reference/Routing/custom-routes#custom-routes-within-the-umbraco-pipeline
regards
marc
Hi Marc, Thanks for the response, my problem was that if I didn't override the compose method to register my controller then I get a different error about it not being registered, don't remember exactly as now doing it the ugly way by passing in a query string parameter due to time constraints, overriding the compose method removed the error but nothing got to the controller.
Cool, so if you add the component to the collection of components in your overriden Compose method - is your route mapped?
and do you need access to Umbraco Content in your View for the custom route, and so is it worth mapping an UmbracoRoute instead?
https://our.umbraco.com/Documentation/Reference/Routing/custom-routes#custom-routes-within-the-umbraco-pipeline
regards
Marc
It would appear it does not get mapped as it never hits the controller method.
I don't need access to Umbraco content, I simply want to pass in an integer which the action uses to pull data from an external source.
I currently have it working using a surfacecontroller action and passing a ?id=XXXX in the query string, but was trying to create a route so that it could be action/id without the need for the query string
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.