Copied to clipboard

Flag this post as spam?

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


  • Nadia 45 posts 122 karma points
    Jun 10, 2019 @ 22:45
    Nadia
    0

    Custom WebApi routes

    I have been going in circles with this issue. I have some custom Webapi routes to be implemented on my Umbraco 8 site.

    I first added a ApplicationStartComponent that called the GlobalConfiguration.Configure(WebApiConfig.Register); . But then I got duplicate error due to this line being in the WebAPIConfig

    config.MapHttpAttributeRoutes();
    

    So I followed the instructions in this issue https://github.com/umbraco/Umbraco-CMS/issues/4775 implemented the following code

    public class ApplicationStartedComposer : IComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Insert<ApplicationStartComponent>();
        }
    }
    public class ApplicationStartComponent : IComponent
    {
        public void Initialize()
        {
    
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            GlobalConfiguration.Configuration.EnsureInitialized();
    
        }
    

    This code meant that the Custom WebAPI works. However when I try and login to the Umbraco backoffice I get a 500 error.

    Any suggestions?

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    Jun 11, 2019 @ 08:19
    Marc Goodson
    100

    Hi Nadia

    Do the custom routes work if you 'just' call MapHttpAttributeRoutes() ?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Core.Composing;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    
    namespace Umbraco8.Components
    {
    
         [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class RegisterCustomApiRoutesComposer : IComposer
        {
     composition.Components().Insert<RegisterCustomApiRoutesComponent>();
        }
    
        public class RegisterCustomApiRoutesComponent : IComponent
        {
    
    
            public void Initialize()
            {
                GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    

    regards

    Marc

  • Nadia 45 posts 122 karma points
    Jun 11, 2019 @ 21:11
    Nadia
    0

    Oh brillant, thanks that worked... was staring at the wrong solution too long :)

  • 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.

Please Sign in or register to post replies