CODE Framework Services can be hosted in a variety of ways, including WebApi. Using WebApi, one typically creates an "ASP.NET App" that contains API Controllers that have methods that are invoked according to certain naming conventions. These methods take JSON and URL parameters as input and create JSON as output. Using CODE Framework extensions, any service can be hosted using the same mechanism.

Assuming a service implementation called CustomerService (perhaps based on an ICustomerService contract/interface), one can host the service in a new controller like this:

public class CustomerController : ServiceHostController<CustomerService>

Note: The ServiceHostController class is defined in the CODE.Framework.Service.Server.WebApi namespace/dll, which needs to be included in your project/file.

Fundamentally, that's all that's needed. The service will now be hosted in the overall environment according to general system configuration. In particular important is the routing setup. By default, ASP.NET creates a routing setup for WebApi (typically found in the WebApiConfig.cs file in the App_Start folder) similar to this:

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

Using this default setup, the CustomerService can now be called using a URL like this:

You can easily change the overall URL pattern in the route configuration. For instance, if you do not want the /api/ part of the URL, you can define the route like this:

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

This results in the following URL:

Note that the default route configuration doesn't allow for many unnamed URL parameters. In fact, it only allows for one ("id"). In many REST scenarios, you may design your services to have additional unnamed URL parameters. You can add as many as you need. Here is an example:

config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}/{id2}/{id3}/{id4}/{id5}", new {id = RouteParameter.Optional, id2 = RouteParameter.Optional, id3 = RouteParameter.Optional, id4 = RouteParameter.Optional, id5 = RouteParameter.Optional});

Note that the full URL each service operation is available at is defined by the service itself. Any REST hosting version of the same service will result in the same URL patterns (not just WebApi). For more information on how REST services are exposed, see: Creating REST Services

Last edited Jan 17, 2015 at 5:45 PM by MarkusEgger, version 6


MarkusEgger Oct 25, 2015 at 7:25 PM 

Sorry... just saw your comment right now.

I am not sure what is going on to be honest. Are you sure you have your routes set up correctly? Do other WebApi services work?


GShock Aug 14, 2015 at 6:20 AM 
Hi, I gave this a shot... I read this article above, and then the Creating REST Services article, and put attributes on my service implementation class' method(s), and i get 404 not found. I am working with the latest release of code framework 4.0.50402.0, and I started a new MVC project to isolate it and I tried with target framework 4.5 and 4.5.2...

any helpful info would be greatly appreciated! thanks and cheers,