Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Warren Harding 132 posts 275 karma points
    Mar 14, 2019 @ 02:53
    Warren Harding
    0

    Custom Routes and ScriptBundles

    Previously in v7 we had some custom events handlers configured which implemented IApplicationEventHandler.

    Then within OnApplicationStarting we'd fire off:

    BundleTable.EnableOptimizations = true;
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    

    And then in OnApplicationStarted we'd have a few:

    RouteTable.Routes.MapRoute(name: "RunController", url: "Run/{action}/{id}", defaults: new { controller = "Run", action = "Index", id = UrlParameter.Optional });
    

    However it looks like ApplicationEventHandler is no longer in Umbraco.Core - could anyone give me some guidance on what to do here?

    Thank you

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 14, 2019 @ 06:45
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 14, 2019 @ 07:39
    Andy Butland
    0

    I've set this up with the following:

    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute...
        }
    }
    
    
    public class RoutingComponent : IComponent
    {
        public void Initialize()
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    
        public void Terminate()
        {
        }
    }   
    
    public class PackageComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<RoutingComponent>();
        }
    }
    

    (Obviously the first and second class could be combined quite easily, but I had a particular reason for breaking it out).

    Namespaces you'll need are:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    

    Andy

  • Warren Harding 132 posts 275 karma points
    Mar 15, 2019 @ 01:29
    Warren Harding
    0

    Thanks for that Andy, however I can't seem to get this to work for creating custom MVC routes... it works great when trying to create new ScriptBundles though, so feel like I'm getting somewhere, thank you!

    Would this be causing problems because BundleConfig.RegisterBundles was being called in OnApplicationStarting while RouteTable.Routes.MapRoute was called in OnApplicationStarted?

    Thanks again, Warren

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 15, 2019 @ 08:33
    Andy Butland
    0

    Not sure I'm afraid. All I would add is that I found I had to register any custom controllers with the IoC container.

    I.e. in PackageComposer.Compose() (above) I've also got:

    composition.Register<CriteriaController>();
    

    Andy

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 15, 2019 @ 08:44
    Søren Gregersen
    0

    You should not have to register Controllers. They are always created via a factory that find them based on the route. The factory may also, as in the case in umbraco 8, be able to resolve dependencies.

    This is definetly a codesmell you should look into.

Please Sign in or register to post replies

Write your reply to:

Draft