Thursday, 5 May 2016

Angular2, ASP.NET WebAPI, Azure & Cordova, Cross Platform

Angular2, ASP.NET WebAPI, Azure & Cordova, Cross Platform – My Private Hackathon Part 2


In the last blogpost I lost a few words about the REST-API the FoodChooser is talking to. In this blog I want to talk about the clients I developed.
Well I started coding an AngularJs 1.x Client with Typecript which is available here
Of course I could not stop looking and digging into Angular2. So I wrote this client too which is available here:

Inside this blog I want to loose a few words about how I wrote them what the pitfalls were and how I came up with this solution.
I will only show Angular2 examples here because it’s the only client which is still maintained

Architecture

The application is divieded into several components with its child components. Due to the fact that the application is not that big at all there is no huge hierarchy.
SiteMap
So the only interesting thing is the Food-Component which has two child Components “FoodForm” and “FoodList”.
The list component itself is not containing the details-view but redirecting to it while iterating through all the foodItems:
and the template

Authentication

The WebAPI is providing a token endpoint to get tokens from after the login process. I do use a “CurrentUserService” to save this token in the storage and read it again.
With this I can read if the user is authenticated in a very basic way.
Further I took a decorator to hook into the creation of components to check if the user is authenticated or not. If not the decorator will redirect to the login page:
With every request I have to prepare the header which I do in a wrapped Http service.
Sneak peek:

So I check the headers, append a token if available, set the content-type and accept-properties and give the options back to use it in the REST-Call.

Cross Platform with Gulp

To give this whole thing a go as an exe and as an app on mobile devices I used cordova and electron with gulp as a taskrunner.
I seperated all the files in the tasks for “electron”, “cordova” and “web”. In the main gulp file I am just gathering all the information and point the default task only to list all available tasks to _not- start something the developer does not know when he only types “gulp” without a specific command.
For example here is the electron gulp file, which turns this application into an exe

For cordova

Conclusion:

I hope with this blog posts I gave you an idea and an introduction in what you can achieve with javascript and ASP.NET WebAPI. Having all the tools like Cordova, gulp. you can build real cross-platform applications.

No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...