Web API

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 30%
  • 6.4
  • Enterprise
  • Community
  • Planned

Overview

ASP.NET Web API is a framework for building HTTP services. It is the currently recommended way of building simple custom REST APIs on top of the .NET Framework. These APIs will be able to serve many different clients, e.g. single page applications, mobile devices, desktop apps. Sense/Net ECM includes a basic support for Web API and this article describes how developers may add their API module to the portal.

Details

Sense/Net ECM virtualizes all paths seen in the browsers address bar. We display our content stored in the Content Repository using smart applications. In the following sections we describe how we were able to make this system work with Web API correctly and let you define your own paths and controllers.

Routing

To provide a way for you to define your controllers and make them work in our environment, we focused mainly on routing. Please check the following article for the details on routing in the Web API:

Examples

Only one thing is necessary to integrate the ASP.NET Web API into the Sense/Net 6 (after the 6.4). This is a routing extension that must be placed in the Configure method of an inherited WebApiConfiguration class. The class can be internal and its name is irrelevant only the origin is important. The Sense/Net will find it via type discovery in the start sequence if the type is presented in an assembly in the web/bin directory. WebApiConfiguration is an abstract class in the SenseNet.Portal.

using SenseNet.Portal;
using System.Web.Http;
using System.Linq;
 
namespace MyWebProject
{
    class MyWebApiConfig : WebApiConfiguration
    {
        protected override void Configure(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
 
            // removing the "application/xml" media type ensures the
            //   JSON output format of every web api response.
            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes
                .FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
        }
    }
}

There is no restricted routing because the Sense/Net 6.4 does not use the ASP.NET Web API.

Let's use a simple controller:

using System.Web.Http;
public class EchoController : ApiController
{
    public object Get()
    {
        return new { message = "EchoController.Get()" };
    }
}

If we call the EchoController according to the defined routing (http://example.com/api/echo), the result will be the following:

{ "message": "EchoController.Get()" }

Effect of routing

There is a restriction if the web API routing is active. In the example the "switch word" is the "api". This name is a reserved word in the root of the requested site. If there is 'api' named content in the requested site's root, this content and every content in the subtree are accessible only with the full path. For example the /api/documents/mydocument.docx can be accessed on the following url: http://example.com/Root/Sites/MySite/api/documents/mydocument.docx (if the current site's path is: "/Root/Sites/MySite").

Related links

References