Tips for Connecting DotNetNuke with Xamarin Mobile Applications

Tips for Connecting DotNetNuke with Xamarin Mobile Applications

DotNetNuke (DNN) is a popular and powerful framework for building .net based web sites and web applications.  Xamarin (now owned by Microsoft) is a powerful cross-platform development tool for building iOS and Android mobile applications.

With versions 7.0 and higher, DNN now supports the powerful and lightweight Web API.  By using this WEB API we can build interfaces to connect DNN with external system such as Xamarin based mobile applications.

The Web API is a framework approach that utilizes HTTP services which can be interfaced from the client side of the web including browsers and mobile devices.  It can be used for RESTful applications with in the .Net framework. Here are a few of the benefits of the ASP.Net Web API:

  1. Strong platform for building RESTful services
  2. Built on top of ASP.NET and supports the ASP.NET request/response pipeline
  3. Maps HTTP verbs to method names
  4. Supports a wide variety of response data including JSON, XML, BSON format.
  5. Can be hosted in IIS, it can be Self-hosted and utilized in any web server that supports .NET 4.0+
  6. Includes a new HttpClient that can communicate with the Web API server. The HttpClient can be utilized in technologies such as ASP.MVC server side, Windows Form applications, Console applications or other applications.

To get started with the DNN to Xamarin interface, you will need to create a RestFul service and place it under your DNN solutions folder.  Follow these steps:

  1. Create a class library project and put it under the Desktop module folder of your DNN site.
    Create a class libraryDesktop module
  2. Change the build path of this solution to the bin folder of your DNNImageChange the build path dnn
  3. Add these references:
  • dll
  • Web.dll
  • ApplicationBlocks.Data.dll
  • Net.Http.dll
  • Net.Http.Formatting.dll
  • Web.Http.dll
  • Json (A namespace that provides classes which are used to implement the core services of Json.NET, which is a popular high-performance JSON framework for .NET)

Be sure to set the make a copy to local to false in the properties.
Newtonsoft json

 

  1. Then add a class, name it ServiceController, and inherit it from DnnApiController à  Like ” public class ServiceController: DnnApiController{}”ServiceController
  2. Add one more class, name it as RouteMapper, and inherit it from IServiceRouteMapperLike ” public class RouteMapper: IServiceRouteMapper{}”RouteMapper
  3. Write a method in RouteMapper Class like below:
    public void RegisterRoutes(IMapRoute mapRouteManager)
    {
    
    mapRouteManager.MapHttpRoute(string ModuleFolderName, string RouteName, string URL, string[] namespace );
    
    // you can write it like the below line
    // mapRouteManager.MapHttpRoute("name of the module folder", "default", "{controller}/{action}", new[] { "name of the namespace" });
    
    }

    Note – this is used for route mapping.  In ASP.NET Web API, a controller is a class that handles the HTTP requests [in our case we have created servicecontroller.cs class for this]. The public methods of the controller are called action methods or simply actions. When the Web API framework receives a request, it routes the request to an action.  When the Web API framework receives an HTTP request, it tries to match the URI against one of the route templates. If no route matches, the client receives a 404 error.

  4. In the ServiceController class you can declare your connection string.
    [ for this you need to add a key value pair in the appsettings tag of web.config file of your dnn solution like below.
    <appSettings>
    <add key="_Con" value="Data Source=ServerName;Initial Catalog=DatabaseName;
    User  ID=userid;Password=password" />
    </appSettings>
    
    Here ServerName and DatabaseName is the sql server and database name of your dnn solution. 
    
    Then get this value in ServiceController class like below.
    string _con = ConfigurationSettings.AppSettings["_Con"].ToString();
    
    and use this value as a connection string for all your SQL operations like the below example:
    
    DataSet ds = SqlHelper.ExecuteDataset(_con, CommandType.Text, “select * from tablename”); ]
  5. In the service Controller class you have to write the HTTP get and HTTP post methods to get and post the data.
  6. For HTTP post method these attributes you can use these:
    [AllowAnonymous]
    [HttpPost]
    [Route("Name of the route")] -- here you need to write the unique name by which you will call the method from your xamarin.forms app.

     

  7. For the HTTP get method these attributes you can use these:
    [AllowAnonymous]
    [HttpGet]
    [Route("Name of the route")] -- here you need to write the unique name by which you will call the method from your xamarin.forms app.

    xamarin forms app

That completes the DNN part of the setup.  Now you will need to use the web service in your Xamarin forms app.

Here is example code to call the method declared in our service example above:

  1. In your app first declare a string value for the path you need to point to like below

string AppURL = “yourwebsiteurl/DesktopModules/Nameofservice/API/Service/”;
declare a string value

  1. Now write a method to do the operation through service like this
    public static string Methodname()
    {
    string rvalue = "";
    if (httpclientStatic == null)
    {
    
    httpclientStatic = new HttpClient(new NativeMessageHandler());
    
    }
    
    httpclientStatic.BaseAddress = new Uri(AppURL);
    
    var response = httpclientStatic.GetAsync(" write here the unique name declared in service for the method");
    
    var reponseContent = response.Result.Content.ReadAsStringAsync();
    
    if (response.Result.IsSuccessStatusCode)
    {
    rvalue = JsonConvert.DeserializeObject<string>(reponseContent.Result);
    }
    return rvalue;
    }
    

     

    write a method
    write a method-2

    Now you have implemented all the necessary steps on both your DNN setup and Xamarin application, you can get, post or modify data in your DNN database from your Xamarin Forms application. We hope this blog guided you through creating an ASP.NET web API and consuming it in a Xamarin.Forms application.