Exposing episerver pagedata using web api

Vote:
 

Hello fellow developers!

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

Thus far I've used:

http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms

and

http://www.frederikvig.com/2012/02/using-the-asp-net-web-api-framework-with-episerver/

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:

            RouteTable.Routes.MapHttpRoute(
                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);
            }
 
            FilterForVisitor.Filter(pages);
 
            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:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://igcpi0d2e007:17006/api/Pages/'.
</Message>
<MessageDetail>
No action was found on the controller 'Pages' that matches the request.
</MessageDetail>
</Error>

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


#72483
Jun 18, 2013 16:21
Vote:
 

Try to reinstall MVC 4, make sure you are not using get instead of post. Make sure IIS can handle all http verbs: http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx

It could also be a namespace issue.

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

<modules runallmanagedmodulesforallrequests="true">

    

#72583
Jun 22, 2013 12:06
Vote:
 

Try adding the ActionApi route as described in Fredriks post.

#72597
Jun 24, 2013 10:07
Vote:
 

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:

http://labs.episerver.com/en/Blogs/Allan/Dates/2010/6/Codemania-Partner-Summit-2010/

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:

http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

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

After putting this in web.config, the data was accessable from ajax on the receiving application.
#72603
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.