SaaS CMS has officially launched! Learn more now.

Cant get web api to work in EPiServer



I am trying to create a web api in my episerver project, but I can't get it to work.  I have done exactly the same as in other episerver projects, where the web api works. But I get this error message when I call the api method:

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

I have a webapiconfig.cs with a register method, where I call this:


And in Global.asax.cs I have this:


and I have also tried adding this:


My controller looks like this:

  public class EventController : ApiController
    public int test()
      return 5;

In postman I call ..../api/test, but get this error message..

Does anyone know what could cause this?

May 08, 2018 12:02

I think from memory this issue can be caused to not having this set up using the Episerver IInitializableModule way. I believe if you are using Find there can be some conflicts with the loading order, although this is off the top of my head. Try putting it in an initalization module such as the following example

/// <summary>
    /// The WEB API Configuration class
    /// </summary>
    public class WebApiConfig : IInitializableModule
        public static string ApiRoute = "api";

        public void Initialize(InitializationEngine context)
            // Enable Web API routing
            GlobalConfiguration.Configure(config =>
                // Attribute routing

                var formatters = GlobalConfiguration.Configuration.Formatters;
                var jsonFormatter = formatters.JsonFormatter;
                var settings = jsonFormatter.SerializerSettings;

                var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();

                settings.Formatting = Formatting.Indented;


                // config.MapHttpAttributeRoutes();

                    name: "DefaultEpiApi",
                    routeTemplate: ApiRoute + "/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional });

        /// <inheritdoc />
        public void Uninitialize(InitializationEngine context)
May 08, 2018 12:14

Thank you so much, Scott, this worked! Do you know why I had to do it this way here, and not in the other projects?

May 08, 2018 12:25

I believe there's a conflict with Episerver Find's own APIs where the configuration.MapHttpAttributeRoutes(); is called in one of these modules and it has an issue if you try to call it twice in the wrong order. By adding the IInitializableModule with the FrameworkInitialization you're making sure this is called after and so it doesn't have the conflict. As far as I remember this is the case anyhow :-)

May 08, 2018 12:42

Ok, I see. I don't use Find, only EPi Search, but i guess there is something causing the same issue. Thanks againsmile

May 08, 2018 12:44

Ah I see, yeah I think as they both expose the same APIs for the CMS/Commerce search facilities they wouldn't probably be doing the same thing. No worries, glad it works :-)

May 08, 2018 12:47

For me I got this when using Marketing (A/B-testing) addon or the service-api since those AddOns does not use the initi... the correct way and have forced in the routing before you do it.

But the way Scott describes works and that is the way we do it also

May 08, 2018 13:58

Aha, we have the A/B testing addon, so it's probably the one causing the problem.

May 08, 2018 14:03
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.