Copied to clipboard

Flag this post as spam?

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


  • Cornelis 12 posts 113 karma points
    Apr 07, 2020 @ 08:29
    Cornelis
    0

    During development, I would like to enable CORS for my WebApi's. Unfortunately, this doesn't want to work as it will when no Umbraco is involved.

    I get this error after installing the cors nuget package:

    The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.

    This is my code:

    public class RegisterCustomApiRoutesComponent : IComponent
    {
        public void Initialize()
        {
            var config = GlobalConfiguration.Configuration;
            config.EnableCors();
            config.MapHttpAttributeRoutes();
            config.EnsureInitialized();
    
        }
    
        public void Terminate()
        {
            throw new System.NotImplementedException();
        }
    }
    

    I couldn't find any (good) instructions on how to enable CORS in my web API. The reason I want this, is that I want to develop and run my angular project on the default angular port (4200) to be able to use it's rapid development features (ng serve). And my API logically is running on a different port.

  • Yakov Lebski 539 posts 2101 karma points
    Apr 07, 2020 @ 14:28
    Yakov Lebski
    101

    I doing it inside Composer and not in component and it works

    public class WebApiSettingsComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
    
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    
    
            var corsAttr = new EnableCorsAttribute("<HOST>", "*", "post,get,..");
            GlobalConfiguration.Configuration.EnableCors(corsAttr);
        }
    }
    
  • Andrew Lansdowne 43 posts 124 karma points
    Oct 18, 2021 @ 13:08
    Andrew Lansdowne
    0

    Thanks , for anyone else struggling to implement this, I had to add a nuget reference to Microsoft.AspNet.WebApi.Cors to make this work.

  • Cornelis 12 posts 113 karma points
    Apr 07, 2020 @ 14:48
    Cornelis
    0

    Thank you Yakov, it works for me too:

    public class RegisterCustomApiRoutesComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            var config = GlobalConfiguration.Configuration;
            config.MapHttpAttributeRoutes();
    
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
    #if DEBUG
            var enableAngularCors = new EnableCorsAttribute("http://localhost:4200", "*", "*");
            config.EnableCors(enableAngularCors);
    #endif
        }
    }
    

    Although I wonder why it works this way and not via the IComponent.

    Because I had a Composer, which looks like this:

    public class RegisterCustomApiRoutesComposer : ComponentComposer<RegisterCustomApiRoutesComponent>
    {
    
    }
    

    And now, I could even eliminate the usage of the class that implements IComponent. How come that the first block of code works, but using the ComponentComposer doesn't?

Please Sign in or register to post replies

Write your reply to:

Draft