Creating vCard action result

Posted by DigiMortal on Weblogs See other posts from Weblogs or by DigiMortal
Published on Sat, 22 May 2010 14:45:49 GMT Indexed on 2010/05/22 14:51 UTC
Read the original article Hit count: 347

Filed under:

I added support for vCards to one of my ASP.NET MVC applications. I worked vCard support out as very simple and intelligent solution that fits perfectly to ASP.NET MVC applications. In this posting I will show you how to send vCards out as response to ASP.NET MVC request.

We need three things:

  1. some vCard class,
  2. vCard action result,
  3. controller method to test vCard action result.

Everything is very simple, let’s get hands on.

vCard class

As first thing we need vCard class. Last year I introduced vCard class that supports also images. Let’s take this class because it is easy to use and some dirty work is already done for us.

NB! Take a look at ASP.NET example in the blog posting referred above. We need it later when we close the topic.

Now think about how useful blogging and information sharing with others can be. With this class available at public I saved pretty much time now. :)


As we have vCard it is now time to write action result that we can use in our controllers. Here’s the code.

public class vCardResult : ActionResult


    private vCard _card;


    protected vCardResult() { }


    public vCardResult(vCard card)


        _card = card;



    public override void ExecuteResult(ControllerContext context)


        var response = context.HttpContext.Response;

        response.ContentType = "text/vcard";

        response.AddHeader("Content-Disposition", "attachment; fileName=" + _card.FirstName + " " + _card.LastName + ".vcf");


        var cardString = _card.ToString();

        var inputEncoding = Encoding.Default;

        var outputEncoding = Encoding.GetEncoding("windows-1257");

        var cardBytes = inputEncoding.GetBytes(cardString);


        var outputBytes = Encoding.Convert(inputEncoding,

                                outputEncoding, cardBytes);


        response.OutputStream.Write(outputBytes, 0, outputBytes.Length);



And we are done. Some notes:

  1. vCard is sent to browser as downloadable file (user can save or open it with Outlook or any other e-mail client that supports vCards),
  2. File name is made of first and last name of contact.
  3. Encoding is important because Outlook may not understand vCards otherwise (don’t know if this problem is solved in Outlook 2010).

Using vCardResult in controller

Now let’s tale a look at simple controller method that accepts person ID and returns vCardResult.

public class ContactsController : Controller


    // ... other controller methods ...

vCardResult vCard(int id)


        var person = _partyRepository.GetPersonById(id);

        var card = new vCard



                    LastName = person.LastName,

                    StreetAddress = person.StreetAddress,

                    City = person.City,

                    CountryName = person.Country.Name,


                    Mobile = person.Mobile,

                    Phone = person.Phone,

                    Email = person.Email,



        return new vCardResult(card);


Now you can run Visual Studio and check out how your vCard is moving from your web application to your e-mail client.


We took old code that worked well with ASP.NET Forms and we divided it into action result and controller method that uses vCard as bridge between our controller and action result. All functionality is located where it should be and we did nothing complex. We wrote only couple of lines of very easy code to achieve our goal. Do you understand now why I love ASP.NET MVC? :)

© Weblogs or respective owner

Related posts about ASP.NET

Related posts about .NET