Developer forum

Forum » Development » How do I implement WebAPI in DW?

How do I implement WebAPI in DW?

Christian Fisker
Reply

With version 8.4.1.0 DW states that:
We are now supporting MVC out of the box – making it possible for developers to develop custom modules using Model View Controllers and WebAPI.

I am looking for a simple example on how to set this up with a simple route and controller.

I know what a route and a controller is, but how do I make it work with Dynamicweb?

Thanks.


Replies

 
Klavs Martens
Reply
This post has been marked as an answer

Hi Christian

We don't have any specific documentation on MVC in Dynamciweb, but here is the code for my presentation from Tech Conf 2014. I contains a WebAPI contrroller. Look in the Controller  folder and Global.asax to folder to find the code for a WebAPI controller and in WebApiConfig.cs to find a sample Route configuration. If you need access to any Dynamicweb API's that requires an active session, you must use RouteHandler = new SessionRouteHandler() in you route configuration and make you client resend the ASP.NET session cookie on every request. If you're calling you own API, then you dont need to worry about sessions.

You'll need a custom solution to get it to work.

Klavs

Votes for this answer: 1
 
Christian Fisker
Reply

Thanks Klavs!

 
António Ramos
Reply

https://vimeo.com/91691704

 
Alec Stubbs
Reply

Hi folks,

I've been trying things out with WebAPI on DW and run into a problem where by I see this error message when navigating to a page:

 

<Error><Message>An error has occurred.</Message><ExceptionMessage>The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace>   at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()
   at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)
   at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)</StackTrace></Error>

 

This happens when I follow Klavs demo and add a WebAPI route. The WebAPI part is working as expected but once I add the route if I navigate to a content managed page using the the friendly name in the URL I see the above error. If I navigate to the page using the page id eg http://localhost/default.aspx?id=1 then the page is displayed properly. If i comment out the WebAPI route the friendly name works again.

 

I've taken the solution files that Klavs included in the thread and it happens in that too.  What am I missing?

I'm using DW 8.6.1.16

Cheers,

Alec

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Did you try calling EnsureInitialized() on your HttpConfiguration object? I have this in my unit tests for web api projects so I assume it's needed for newer versions of Web API and it seems that in "standard" web api projects its able to call this method itself.

Imar

 
Alec Stubbs
Reply

Hi Imar thanks for your reply. Yeah I tried putting GlobalConfiguration.Configuration.EnsureInitialized(); at the end of my Application_Start method as the error suggests but I still had the same problem.

 

You must be logged in to post in the forum