Using the API App Orchestration through different web technologies (Part 2)

7:15 PM
Using the API App Orchestration through different web technologies (Part 2) -

Hope you had a chance to take a look at Part 1 to see how the use the API App Orchestration using webforms. In the next part of the series, we will look to do the same, but this time using MVC. MVC is the latest and Microsoft's ASP technology, mixing C # / VB syntax language with basic HTML elements. Things look much cleaner and are easier to manage in MVC and webforms old school. It is about as good as you can get help from server-side scripts. (If you do not let the Microsoft umbrella :.))

The created project I was using Visual Studio 2012 using MVC 4. Visual Studio has a fine on the box project that uses integrated Windows authentication. This allows us to demostrate another way to access the API - the ImplictAuthContext. I just added the ApiContext my HomeController, but in a more complex example you want to store it separately and access from a base controller class to not constantly installing and dismantling of context each controller initialization.

Let's take a look at the code for this:

 public virtual async Task  Index () {ApiContext = new ImplicitAuthContext (DefaultApiServiceAddress); ApiContext.GlobalSettings.Get global var = (); ViewBag.Message = string.Format ( "Are we licensed {0}", Global.IsLicensed?) 

Note two things here:

  1. The keyword async . the API allows you to enjoy this new .NET functionality and make asynchronous calls. (More on that in a moment.)
  2. you can store the results in the ViewBag then display them on the view. Here, I show a simple query to check the status of the App Orchestration license. you never want to be without a license, right? :)
  3. this is all you have need with the implicit method. - a simple call of a line We stock a controller property

in the example of webforms we obtained data offerings and linked them to a repeater. . in MVC, we question them and save them in a template for display.

This is the query. You see it uses async. After waiting for the results, I then massage the data query in my specific model using LINQ

 var = offeringsTask ApiContext.Offerings.GetAsync (). offers var = (waiting offeringsTask) .ToList (); offeringModels var = (to offer the offers select new {Id = OfferingModel offering.Id, Name = offering.Name, IconURL = Url.LargeIconUrl (offering.Icon.Id)}) OrderBy (ad => ad. name). var = new model HomeModel Offerings = {offeringModels.ToList ()}; return View (model); 

Here the reference models:

 public class OfferingModel {public string Id {get; together; } Public string IconURL {get; together; } Public string Name {get; together; }} 
 public class HomeModel {public List  Offerings {get; together; }} 

This is all the code you need in the cshtml page to browse offers:

     offers     @foreach (var offers in Model.Offerings) {      @ offering.Name   }   

and it will look like, the same as the example of webforms:

MVC syntax made to the many table easier and it almost seems like good old HTML.

The way we do the icons is not much different, but it is easier and simpler. First, there is this beautiful helper class that builds the urls to get the icon images.

 UrlHelperExtensions public static class {public static string LargeIconUrl (this UrlHelper urlHelper, Guid? Id) {return urlHelper.Action ( "GetIcon", "Home", new {id, width = 32 height = 32 }); } Public static string LargeIconUrl (this UrlHelper urlHelper, string id) {return urlHelper.Action ( "GetIcon", "Home", new {id, width = 32 height = 32}); }} 

Then we have the typical method that returns the actual bits of the image. (This is roughly the same as the example of webforms but you can build it directly into your controller. However, you may want it to be a separate controller in a real world example to isolate concerns.)

 [OutputCache(Duration = Int32.MaxValue)] // Cache forever public virtual async Task  GetIcon (string id, int? width, int? height) {try {var icon = wait ApiContext.Icons.GetByIdAsync (id, IconFormat. png, width ?? 32, ?? height 32); return file (Convert.FromBase64String (icon.DataBase64), "image / png"); } Catch (Exception e) {Trace.TraceError ( "Error Message obtaining Icon: {0}", e.Message); return new EmptyResult (); }} 

There. This project ended up with less code, was easier to read and follow some of the latest techniques server side. In part 3, we will change this example not to use server-side processing, but instead use knockout and Ajax requests to get the data. Stay tuned!

Previous
Next Post »
0 Komentar