DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Trending

  • Exploring Edge Computing: Delving Into Amazon and Facebook Use Cases
  • Microfrontends for Quarkus Microservices
  • Writing Reusable SQL Queries for Your Application With DbVisualizer Scripts
  • Reflections From a DBA

Creating vCard action result

Gunnar Peipman user avatar by
Gunnar Peipman
·
May. 25, 10 · News
Like (0)
Save
Tweet
Share
4.93K Views

Join the DZone community and get the full member experience.

Join For Free

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. :)

vCardResult

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

ublic 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 ...


public vCardResult vCard(int id)

{

var person = _partyRepository.GetPersonById(id);

var card = new vCard

{

FirstName=person.FirstName,

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.

Conclusion

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? :)

 

ASP.NET MVC

Opinions expressed by DZone contributors are their own.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: