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.
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);
}
}
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?
Enable CORS for webApi
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:
This is my code:
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.
I doing it inside Composer and not in component and it works
Thanks , for anyone else struggling to implement this, I had to add a nuget reference to Microsoft.AspNet.WebApi.Cors to make this work.
Thank you Yakov, it works for me too:
Although I wonder why it works this way and not via the IComponent.
Because I had a Composer, which looks like this:
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?
is working on a reply...