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

8 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

#5 Shawn on 08.04.10 at 11:54 am

Do either of you have additional working samples or guidance on user-authenticating against HealthVault using MVC?

#6 vaibhavb on 08.04.10 at 11:57 am

hi Shawn – By user-authenticating you mean implementing the redirection targets and work-flow, right? If you provide tad bit more info, I’ll try to help. regards, Vaibhav

#7 Shawn on 08.05.10 at 1:26 pm

Vaibhav, Yes. Originally, that was what I meant but I have worked through it using the HealthVaultController technique you mentioned.

Now that I can login/logout of HealthVault via my mvc app, I am venturing into creating the models. Would you have any advice for me in that area? Thankfully, this particular app only reads health information from the users HV record.

Any guidance is much appreciated.
Shawn

#8 vaibhavb on 08.06.10 at 4:33 pm

hi Shawn -

Since you are just reading the data, may be you can create wrapper classes on the existing HealthVault SDK HealthRecordItems and pass the wrappers directly to the view.

regards,
Vaibhav

Leave a Comment