Talking to HealthVault via ASP.NET MVC #1

Recently the ASP.NET MVC framework came out of beta and has garnered a lot of developer interest. This framework tries to provide interesting things like – total control over HTML, human readable URLs, AJAX, and facilitates test driven development. Successful web development frameworks like Ruby on Rails (ruby), Django (python) and Spring (Java) – also enable these characteristics. So cleary, there is some traction and tread for web frameworks to facilitate the Model, View, Controller paradigm.

There are quite a few challenges with regards to HealthVault SDK to work in this realm. The standard HealthServicePage no longer works in a controller paradigm. Eventually the SDK will need to provide a HealthServiceController, but until then I’ll try and explain how we can use the current SDK to work in an ASP.NET MVC world.

As I have explained earlier in my ruby series HealthVault authenticates an application in three contexts – anonymous, the application itself and the application in presence of user information. In this series I will try to take the same approach and explain how we can do the same with ASP.NET MVC.

It’s relative easy to talk to HealthVault via the anonymous GetServiceDefination method. Here is the heart of the code which enables this:

   1: public ActionResult Index()
   2: {
   3:     ViewData["Message"] = "Welcome to HealthVault ASP.NET MVC!";
   4:     
   5:     // Do an anonymous connection with HealthVault using the Hello World application id
   6:     ApplicationConnection appConnection = new ApplicationConnection(
   7:         new Guid("05a059c9-c309-46af-9b86-b06d42510550"),
   8:         "https://platform.healthvault-ppe.com/platform/");
   9:     // Get the service defination from HealthVault platform
  10:     ServiceInfo sInfo = appConnection.GetServiceDefinition();
  11:     // Pass the data for viewing
  12:     ViewData["ServiceInfo"] = sInfo.HealthServiceUrl.ToString();
  13:     return View();
  14: }


Here is the output from running this sample Mvc application, the entire source code for this application is shared here.

HealthVault Mvc #1

Next time: How to authenticate your application with HealthVault through ASP.NET MVC

4 comments ↓

#1 brian on 07.17.09 at 12:08 pm

howdy,
in the middle of doing this myself – maybe we can share experiences… i’ll add that i have changed references to Page to HealthServicePage and references to ActionPage to HealthServiceActionPage – this allows you authenticate upon hitting the site. currently figuring out how to pass auth credentials into the controllers…

#2 brian on 07.20.09 at 7:42 am

additionally there were a variety of tweaks that i had to make to the web.config file to get things pointing in the right direction. i have no good place for hosting this information at the moment but would be happy to share with anyone interested…

#3 brian on 07.20.09 at 12:04 pm

Ok the remaining pieces to this puzzle seem to be as follows:

- insert the following line into _Default.Page_Load.aspx.cs:

Session["HttpContext"] = this.Context;

- Extend the class Sytem.Web.Mvc.Controller
- add to it’s base constructor the following lines:

HttpContext context = (HttpContext)Session["HttpContext"];
PersonInfo info = WebApplicationUtilities.PageOnPreLoad(context, true);

- Expose PersonInfo as a property of MyController or whatever you’ve named it.

This might just do the trick…

#4 vaibhavb on 07.21.09 at 4:23 pm

hi Brian -

This is awesome! Haven’t gotten the chance to try this approach. I was thinking of going the route of not using session object and trying to figure out the HealthVault auth token redirect through HealthVaultController class.

regards.
Vaibhav

Leave a Comment