0

I am on Sitecore 9.1.

I have read this question and this other one; my configuration is as follows.

In order to execute my code on startup, I hijack the initialize pipeline:

<pipelines>
  <initialize>
    <processor patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']"
      type="MyProject.Pipelines.RegisterHttpRoutes.WebApiConfig, BlueRubicon.Pipelines" />
  </initialize>
</pipelines>

My Global.asax is the default one:

<%@Application Language='C#' Inherits="Sitecore.Web.Application" %>

And I have no Global.asax.cs.

This is the WebApiConfig class:

public class WebApiConfig
{
    public void Process(PipelineArgs args)
    {
        GlobalConfiguration.Configure(Register);
    }

    public void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute("FileUploadApi", "api/FileUpload/{id}",
            new { controller = "FileUpload", id = RouteParameter.Optional });

        config.Routes.MapHttpRoute("CareersApi", "api/careersapi/{action}/{id}",
            new { controller = "CareersApi", id = RouteParameter.Optional });

        config.Routes.MapHttpRoute("Inspiration", "api/inspiration/{action}/{id}",
            new { controller = "Inspiration", id = RouteParameter.Optional });

        config.Routes.MapHttpRoute("Brand", "api/Brand/{action}/{id}",
           new { controller = "Brand", id = RouteParameter.Optional });
    }
}

Question

Why am I still getting the The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code. error?

2

Are you just trying to register API routes? Its way easier than that.

code

public class InitializeRoutes
{
    public virtual void Process(PipelineArgs args)
    {
        // Registration
        RouteTable.Routes.MapRoute("create-member",
            Constants.BaseRoute
            + Constants.ApiVersion
            + "/membership",
            new { controller = "RegistrationApi", action = "Create" },
            new { httpMethod = new HttpMethodConstraint("POST") }
        );
    }
}

config

<pipelines>
  <initialize>
    <processor type="YourApp.Pipelines.Initialize.InitializeRoutes, YourApp" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
  </initialize>
</pipelines>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.