DotNetNuke 7 Web Services Framework Web API Routes


When building the DotNetNuke Web Services API controllers, I want to use this REST convention for the endpoints to access a resource such as the Chinook database. Note: My DotNetNuke website is running under IIS 7.5 with "dnn7" as the host name binding. Only the request method, url, protocol, content-type and content-body are included in request examples below.

Return all Tracks:

GET http://dnn7/DesktopModules/Services/API/Tracks HTTP/1.1

Return a Track where ID = 1:

GET http://dnn7/DesktopModules/Services/API/Tracks/1 HTTP/1.1
CREATE a Track:
POST http://dnn7/DesktopModules/Services/API/Tracks HTTP/1.1

Content-Type: application/json

{
    "albumId": 1,
    "name": "For Those About To Rock (We Salute You)",
    "mediaTypeId": 1,
    "genreId": 1,
    "composer": "Angus Young, Malcolm Young, Brian Johnson",
    "milliseconds": 343719,
    "bytes": 11170334,
    "unitPrice": 0.99
}
UPDATE a Track:
PUT http://dnn7/DesktopModules/Services/API/Tracks/1 HTTP/1.1

Content-Type: application/json

{
    "albumId": 1,
    "name": "For Those About To Rock (We Salute You)",
    "mediaTypeId": 1,
    "genreId": 1,
    "composer": "Angus Young, Malcolm Young, Brian Johnson",
    "milliseconds": 343719,
    "bytes": 11170334,
    "unitPrice": 0.99
}
DELETE a Track where ID = 1:
DELETE http://dnn7/DesktopModules/Services/API/Tracks/1 HTTP/1.1
DELETE Tracks – body contains a json array of Track ID’s to delete:
DELETE http://dnn7/DesktopModules/Services/API/Tracks/Delete HTTP/1.1

Content-Type: application/json

{
    "ids": [
        6,
        7,
        8
    ]
}
RouteMapper.cs
using System;
using System.Web.Http;
using DotNetNuke.Web.Api;

namespace WebServices
{
    public class RouteMapper : IServiceRouteMapper
    {
        public void RegisterRoutes(IMapRoute mapRouteManager)
        {
            mapRouteManager.MapHttpRoute(
                moduleFolderName: "Services",
                routeName: "Default",
                url: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new[] { "WebServices" }
            );
        }
    }
}

Json.NET

For the Delete method in the TracksController, we will use Json.NET to parse the track id’s from the json payload. To install Json.NET into the solution, I prefer NuGet. From the menu In Visual Studio, select Tools > Library Package Manager > Package Manager Console. Then run the following command:

PM> Install-Package Newtonsoft.Json
TracksController.cs
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DotNetNuke.Web.Api;
using Newtonsoft.Json.Linq;

namespace WebServices
{
    [AllowAnonymous]
    public class TracksController : DnnApiController
    {
        public HttpResponseMessage Get()
        {
            //put your code here for call to handle the get
            //for example
            //tracks = TracksService.Get();

            return Request.CreateResponse(HttpStatusCode.OK, tracks);
        }

        public HttpResponseMessage Get(int id)
        {
            //put your code here for call to handle the get
            //for example
            //track = TracksService.Get(id);

            return Request.CreateResponse(HttpStatusCode.OK, track);
        }

        [DnnAuthorize]
        public HttpResponseMessage Post(Track track)
        {
            //put your code here for call to handle the create
            //for example
            //TracksService.Create(track);

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [DnnAuthorize]
        public HttpResponseMessage Put(Track track)
        {
            //put your code here for call to handle the update
            //for example
            //TracksService.Update(track);

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [DnnAuthorize]
        public HttpResponseMessage Delete(int id)
        {
            //put your code here for call to handle the delete
            //for example
            //TracksService.Delete(id);

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [DnnAuthorize]
        public HttpResponseMessage Delete(JObject jObject)
        {
            var ids = new JArray();
            if (jObject["ids"].Type == JTokenType.String)
            {
                ids.Add(jObject["ids"]);
            }
            else
            {
                ids = (JArray)jObject["ids"];
            }
            long[] assetIds = ids.Select(jv => Convert.ToInt64((string)jv)).ToArray();
            
            //put your code here for call to handle the delete
            //for example
            //TracksService.Delete(assetIds);

            return Request.CreateResponse(HttpStatusCode.OK);
        }

    }

}

Resources

Published by

Jim Frenette

Web Developer - views here are my own except those taken from people more clever than me.

Loading Disqus Comments ...
Loading Facebook Comments ...