Try our conversational search powered by Generative AI!

Exposing episerver pagedata using web api


Hello fellow developers!

I'm creating my first Episerver solution using web api to expose data as json code.

Thus far I've used:


to enable web api in my episerver solution. I was not able to make it work in my orginal solution

so I moved everything into a brand new episerver solution and there it worked (in a way).

I've added the following code to Application_Start in Application_Start in Global.asax.cs:

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = System.Web.Http.RouteParameter.Optional }
I've added the following web api class to a directory called Controller:
namespace Solution.Controller
    public class PagesController : ApiController
        public PageDataView Get(int id)
            PageData page = DataFactory.Instance.GetPage(new PageReference(id));
            if (page == null)
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            if (!page.CheckPublishedStatus(PagePublishedStatus.Published) || !page.QueryDistinctAccess(AccessLevel.Read))
                throw new HttpResponseException(System.Net.HttpStatusCode.Forbidden);
            return page.ConvertToPageDataView();
        public IEnumerable<PageDataView> GetChildren(int id)
            var pages = DataFactory.Instance.GetChildren(new PageReference(id));
            if (pages == null)
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            if (pages.Count == 0)
                throw new HttpResponseException(System.Net.HttpStatusCode.Forbidden);
            return pages.Select(p => p.ConvertToPageDataView());

When I try http://igcpi0d2e007:17006/api/Pages/ I get:
No HTTP resource was found that matches the request URI 'http://igcpi0d2e007:17006/api/Pages/'.
No action was found on the controller 'Pages' that matches the request.

When I try http://igcpi0d2e007:17006/api/Pages/175 I get:

<Error><Message>An error has occurred.</Message><ExceptionMessage>Multiple actions were found that match the request: 
Vilkaar.ViewModels.PageDataView Get(Int32) on type Vilkaar.Controller.PagesController
System.Collections.Generic.IEnumerable`1[Vilkaar.ViewModels.PageDataView] GetChildren(Int32) on type Vilkaar.Controller.PagesController</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace>   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace></Error>
And when I try http://igcpi0d2e007:17006/api/Pages/Get/175 I get 404 error.

I really hope someone can help me because customers are waiting for a solution.
In advance thank you very much for clues in how to fix this problem.

Kind regards,
Jon Haakon

Jun 18, 2013 16:21

Try to reinstall MVC 4, make sure you are not using get instead of post. Make sure IIS can handle all http verbs:

It could also be a namespace issue.

You may also need to check in web.config if you have this:

<modules runallmanagedmodulesforallrequests="true">


Jun 22, 2013 12:06

Try adding the ActionApi route as described in Fredriks post.

Jun 24, 2013 10:07

Thank you very much for your replies! I really appreciate it!

This has been resolved. I solved it using my first solution in providing json data.

I first tried to use Allan Thræns solution:

but it was not possible to access the data with ajax on the receiving solution.

I was told that I should use web api to solve this, but my sample jsondata was not possible to access either.

Finally we found the solution to this problem that was this:

   <add name="Access-Control-Allow-Origin" value="*" />

After putting this in web.config, the data was accessable from ajax on the receiving application.
Jun 24, 2013 13:08
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.