It is a really good news for the DNN lover, on version 7.0 has now supported the powerful and light weight Web API. Although the DNN still use the web forms technology which has been outweighed by ASP.Net MVC, I am still thinking it is still a great technology as until the latest version, it is still supported by Microsoft.
In order to enable the Web API, you will need to add the following dll reference into your project. The first two dll can referenced from the bin folder of your DotNetNuke folder. The files you need to reference are:
- DotNetNuke.dll
- DotNetNuke.Web.dll
- System.Net.Http.dll
- System.Net.Http.Formatting.dll
- System.Web.Http.dll
Once you have done this, you will need to register your controller class. Please make sure that you reference DotNetNuke.Web.Api and inherits the IServiceRouteMapper.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using DotNetNuke.Web.Api; namespace MyModuleName.WebAPI { public class RouterMapper : IServiceRouteMapper { public void RegisterRoutes(IMapRoute mapRouteManager) { mapRouteManager.MapHttpRoute("MyModuleName.WebAPI", "default", "{controller}/{action}", new[] { "MyModuleName.WebAPI" }); } } }
Once the routing mapper for your controller has already been created. We can start creating your first Web API service. You will learn how to create Web Service API that will accept a post and get object. In addition, you will learn how to give access to Web API for logged in users only.
using System; using System.Collections.Generic; using System.Web; using System.Web.Http; using DotNetNuke.Web.Api; using System.Net.Http; using System.Net; using System.Text; namespace MyModuleName.WebAPI { public class HelloWorldController : DnnApiController { [HttpPost] public HttpResponseMessage PostMyName([FromBody] yourName) { try { return Request.CreateResponse(HttpStatusCode.OK, "Hello World " + yourName); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } } [HttpGet] public HttpResponseMessage GetMyName([FromBody] yourName) { try { return Request.CreateResponse(HttpStatusCode.OK, "Hello World " + yourName); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } } [HttpPost] [DnnAuthorize] public HttpResponseMessage PostMyName([FromBody] PersonInfo objPerson) { try { return Request.CreateResponse(HttpStatusCode.OK, "Hello World " + objPerson.FirstName + " " + objPerson.LastName); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } } } }
If you see above example, you can see to determine if a Web API service only apply for post or get only is by identifying the header [HttpPost] and [HttpGet]. While to make the service only can be accessed by logged user is by specifying [DnnAuthorize] on above of your method code.
Once you do the Web Service API class you can try the DNN Web Service by using JQuery script to perform post and get action. See below example for more details. What you need to remember on the Web API URL in DNN is you need to follow this format:
/desktopmodules/namespace-of-your-module/API/controller-name/method-name
//Example of using POST var jsonData = { FirstName: 'Brian', LastName: 'James' } $.ajax({ url: "/desktopmodules/MyModuleName.WebAPI/API/HelloWorld/PostMyName", cache: false, type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(jsonData), success: function (response) { console.log(response); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.responseText); } }) //Example of using GET var myName = 'James'; $.ajax({ type: "GET", url: "/desktopmodules/MyModuleName.WebAPI/API/HelloWorld/GetMyName?yourName" + myName, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { console.log(response); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.responseText); } });