November Happy Hour will be moved to Thursday December 5th.

Route /api/episerver/v2.0/content not found (but /api/episerver/v2.0/site is)

Vote:
 

Hello,

I'm trying to enable the Content Delivery Api in my application, but it seems I have a problem with routing. I simply added the NuGet packages "EPiServer.ContentDeliveryApi.Cms" and "EPiServer.ContentDeliveryApi.Core" to my project. I then tested this configuration by making a call from a browser (authenticated) from JavaScript. The call looks like follows:

// jQuery
$.ajax({
   url: "/api/episerver/v2.0/content",
   type: "GET",
   beforeSend: function(xhr){
      xhr.setRequestHeader('Accept', 'application/json');
      xhr.setRequestHeader('Accept-Language', 'nl');
   }
});

A call to "/api/episerver/v2.0/site" does work:

// jQuery
$.ajax({
   url: "/api/episerver/v2.0/site",
   type: "GET",
   beforeSend: function(xhr){
      xhr.setRequestHeader('Accept', 'application/json');
      xhr.setRequestHeader('Accept-Language', 'nl');
   }
});

I know that the content delivery api also generated 404 responses if the content is not found or maybe in some other cases, but I don't think this is the case. I added the following to make sure the request was handled by a route:

// Global.asax.cs
public override void Init()
{
    base.Init();
    this.AcquireRequestState += ShowRouteValues;
}

protected void ShowRouteValues(object sender, EventArgs e)
{
    var context = System.Web.HttpContext.Current;
    if (context == null)
        return;
    var routeData = RouteTable.Routes.GetRouteData(new System.Web.HttpContextWrapper(context));
    // routeData can now be inspected with the debugger
}

When inspecting routeData after a JavaScript call to "/api/episerver/v2.0/content" routeData is null. When I explicitly add a route for this URL the routeData is not null:

protected override void RegisterRoutes(RouteCollection routes)
{
    base.RegisterRoutes(routes);
    routes.MapRoute("test", "api/episerver/v2.0/content", new {
        controller = "ContentApi",
    });
    // ...
}

When I make an override controller in my own code where I just call the base everywhere and set the same attributes when everything works fine.

To me it seems that the Route attributes in the ContentApiController class in the EPiServer.ContentDeliveryApi.CMS assembly are not found, but the strange thing is dat the sibling controller (SiteDefinitionApiController) is.

Anyone any ideas on how to get the routes for the content delivery api to work?

Thanks!

#205521
Jul 15, 2019 18:11
Vote:
 

I had the same issue, adding the key mentioned in Quan's Accepted Answer helped.
https://world.episerver.com/forum/developer-forum/Developer-to-developer/Thread-Container/2018/11/content-delivery-api---404-error/

This was the overall setup guide I found most helpful
https://world.episerver.com/blogs/david-buo/dates/2018/11/get-started-with-content-delivery-api-2-2-0/ 

Also, don't forget:

  • Give your contentapiread user Read access to Root
  • Reindex Find, if you plan to use the Search API (as I did recently in my Blazor + Episerver Content Delivery API solution, I'll be sharing the source of this soon).
#205530
Edited, Jul 15, 2019 23:17
Vote:
 

Thanks Darren, but unfortunately I already had the following keys in my Web.config, combined with my own call to configuration.MapHttpAttributeRoutes():

<configuration>
  <appSettings>
    <add key="episerver:contentdelivery:maphttpattributeroutes" value="false" />
    <add key="episerver:contentdeliverysearch:maphttpattributeroutes" value="false" />
  </appSettings>
  <!-- ... -->
</configuration>

I have tried every combination of these settings and commented my own call to MapHttpAttributeRoutes when one of these settings was true.

I also use an initialization module:

[ModuleDependency(typeof(ContentApiCmsInitialization))]
public class ContentDeliveryApiInitialization : IConfigurableModule
{
    public void Initialize(InitializationEngine context)
    {
        //Add initialization logic, this method is called once after CMS has been initialized
    }

    public void Uninitialize(InitializationEngine context)
    {
        //Add uninitialization logic
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        // set minimumRoles to empty to allow anonymous calls (for visitors to view site in view mode)
        context.Services.Configure<ContentApiConfiguration>(config =>
        {
            config.Default().SetMinimumRoles(string.Empty);
        });
    }
}

But I do think this has mainly to do with some problem with the routing.

#205538
Edited, Jul 16, 2019 9:29
Vote:
 

Hi,

404 error could be caused by many reasons. please check your handlers in webconfig

https://world.episerver.com/forum/developer-forum/Addons/Thread-Container/2019/5/headless-api---404-not-found/

#205540
Jul 16, 2019 9:48
Vote:
 

Hi Twan,

Try commenting out the first key in Web.config

I found when I have both keys Web.config I get the 404 error requesting any of the /content URLs

  • /api/episerver/v2.0/content/5
  • /api/episerver/v2.0/content/5/children
  • /api/episerver/v2.0/content/5/ancestors

See below my request in Postman that returns 404.

But after I comment out the first key in Web.config. 

<!--<add key="episerver:contentdelivery:maphttpattributeroutes" value="false" />-->
<add key="episerver:contentdeliverysearch:maphttpattributeroutes" value="false" />

Postman returns the JSON result expected.

Hope this helps, my local is a clean install of Alloy + Episerver Content Delivery API.

But as Quan said, there are could be other things going on in your particular setup.

#205557
Edited, Jul 16, 2019 12:09
Vote:
 

Excellent Darren, that could be also the main reason.

#205575
Jul 17, 2019 4:36
* 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.