SaaS CMS has officially launched! Learn more now.

Cant get web api to work in EPiServer

Vote:
 

Hi!

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:

configuration.MapHttpAttributeRoutes();

And in Global.asax.cs I have this:

GlobalConfiguration.Configure(WebApiConfig.Register);

and I have also tried adding this:

GlobalConfiguration.Configuration.EnsureInitialized();

My controller looks like this:

[RoutePrefix("api")]
  public class EventController : ApiController
  {
    [HttpGet]
    [Route("test")]
    public int test()
    {
      return 5;
    }
  }

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

Does anyone know what could cause this?

#192135
May 08, 2018 12:02
Vote:
 

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>
    [InitializableModule]
    [ModuleDependency(typeof(FrameworkInitialization))]
    public class WebApiConfig : IInitializableModule
    {
        public static string ApiRoute = "api";

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

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

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

                settings.Formatting = Formatting.Indented;

                config.Formatters.Remove(config.Formatters.XmlFormatter);

                // config.MapHttpAttributeRoutes();

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

        /// <inheritdoc />
        public void Uninitialize(InitializationEngine context)
        {
        }
    }
#192136
May 08, 2018 12:14
Vote:
 

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?

#192138
May 08, 2018 12:25
Vote:
 

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 :-)

#192139
May 08, 2018 12:42
Vote:
 

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

#192140
May 08, 2018 12:44
Vote:
 

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 :-)

#192141
May 08, 2018 12:47
Vote:
 

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

#192149
May 08, 2018 13:58
Vote:
 

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

#192151
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.