In this article I will explain with an example, how to create and download vCard (.VCF) file in ASP.Net Core (.Net Core) MVC.
Note: For beginners in ASP.Net Core (.Net Core 7) MVC, please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Image Folder (Directory) Location

The Image file is located inside the Images Folder of wwwroot Folder (Directory).
ASP.Net Core: Create and download vCard (.VCF) file
 
 

Model

The Model class consist of following properties.
public class ContactModel
{
    public string Name { getset; }
    public string Organization { getset; }
    public string Title { getset; }
    public string MobileNumber { getset; }
    public string HomeNumber { getset; }
    public string WorkNumber { getset; }
    public string EmailAddress { getset; }
    public string Website { getset; }
    public string ImageUrl { getset; }
}
 
 

Namespaces

You will need to import the following namespace.
using System.Text;
 
 

Controller

Inside the Controller, first the private property of IWebHostEnvironment interface is created.
Then, the IWebHostEnvironment interface is injected into the Constructor (HomeController) using Dependency Injection method and the injected object is assigned to the private property of IWebHostEnvironment interface.
The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, the ContactModel class properties are set with the appropriate values and returned to the View.
 

Action method for handling POST operation

Inside this Action method, first a StringBuilder class object is created.
Then, the information for creating the vCard (.VCF) is read from their respective fields and appended to the StringBuilder class object using Append method.
For the contact Photo, image file is read from the Folder (Directory) named Images using IWebHostEnvironment interface.
Note: For more details about IWebHostEnvironment interface, please refer Using IWebHostEnvironment in ASP.Net Core. For more details on how to access Static files in ASP.Net Core (.Net Core 7) MVC, please refer my article .Net Core 7: Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Then, image file is converted into BYTE Array and then converted to BASE64 string which is ultimately appended to the StringBuilder class object.
Once the vCard details such as Name, Organization, Phone Numbers, Email Addresses, Websites and Photo of the contact is filled, the file is downloaded using the File function.
Note: The ContentType of Response class is set as text/vcard which informs the Browser that the file type is a vCard.
 
public class HomeController : Controller
{
    private IWebHostEnvironment Environment;
    public HomeController(IWebHostEnvironment environment)
    {
        this.Environment = environment;
    }
    public IActionResult Index()
    {
        ContactModel contactModel = new ContactModel();
        contactModel.Name "Mudassar Khan";
        contactModel.Organization "ASPSnippets Private Limited";
        contactModel.Title "Director";
        contactModel.MobileNumber "9800000000";
        contactModel.HomeNumber "6300000000";
        contactModel.WorkNumber "7800000000";
        contactModel.EmailAddress "mudassar.khan@aspsnippets.com";
        contactModel.Website "www.mudassarkhan.com";
        contactModel.ImageUrl "\\Images\\Mudassar.png";
        return View(contactModel);
    }
 
    [HttpPost]
    public IActionResult Index(ContactModel contactModel)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("BEGIN:VCARD\r\nVERSION:2.1");
        sb.Append(System.Environment.NewLine);
 
        sb.Append("N:");
        sb.Append(contactModel.Name.Split(' ')[0]);
        sb.Append(";");
        sb.Append(contactModel.Name.Split(' ')[1]);
        sb.Append(";");
        sb.Append(System.Environment.NewLine);
 
        sb.Append("FN:");
        sb.Append(contactModel.Name);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("TEL;CELL:");
        sb.Append(contactModel.MobileNumber);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("TEL;HOME:");
        sb.Append(contactModel.HomeNumber);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("TEL;WORK:");
        sb.Append(contactModel.WorkNumber);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("EMAIL;WORK:");
        sb.Append(contactModel.EmailAddress);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("ORG:");
        sb.Append(contactModel.Organization);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("TITLE:");
        sb.Append(contactModel.Title);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("URL:");
        sb.Append(contactModel.Website);
        sb.Append(System.Environment.NewLine);
 
        string folderPath = this.Environment.WebRootPath + contactModel.ImageUrl;
        byte[] bytes = System.IO.File.ReadAllBytes(folderPath);
        string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
        sb.Append("PHOTO;ENCODING=BASE64;JPEG:");
        sb.Append(base64);
        sb.Append(System.Environment.NewLine);
 
        sb.Append("END:VCARD");
 
        return File(Encoding.ASCII.GetBytes(sb.ToString()), "text/vcard", "Mudassar.vcf");
    }
}
 
 

View

HTML Markup

Inside the View, in the very first line the ContactModel class is declared as model for the View and the ASP.Net TagHelpers is inherited.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The HTML Form consists of an HTML Table which consists of an Image element and some Hidden Fields and a Submit Button.
@model Create_Download_vCard_Core.Models.ContactModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td rowspan="10" valign="top">
                    <input type="hidden" asp-for="ImageUrl" />
                    <img src="@Model.ImageUrl" />
                </td>
            </tr>
            <tr>
                <td style="width: 100px">Name:</td>
                <td>
                    <input type="hidden" asp-for="Name" />
                    @Model.Name
                </td>
            </tr>
            <tr>
                <td style="width: 100px">Company Name:</td>
                <td>
                    <input type="hidden" asp-for="Organization" />
                    @Model.Organization
                </td>
            </tr>
            <tr>
                <td style="width: 100px">Title (Position):</td>
                <td>
                    <input type="hidden" asp-for="Title" />
                    @Model.Title
                </td>
            </tr>
            <tr>
                <td>Mobile Number:</td>
                <td>
                    <input type="hidden" asp-for="MobileNumber" />
                    @Model.MobileNumber
                </td>
            </tr>
            <tr>
                <td>Home Number:</td>
                <td>
                    <input type="hidden" asp-for="HomeNumber" />
                    @Model.HomeNumber
                </td>
            </tr>
            <tr>
                <td>Work Number:</td>
                <td>
                    <input type="hidden" asp-for="WorkNumber" />
                    @Model.WorkNumber
                </td>
            </tr>
            <tr>
                <td>Email Address:</td>
                <td>
                    <input type="hidden" asp-for="EmailAddress" />
                    @Model.EmailAddress
                </td>
            </tr>
            <tr>
                <td>Website:</td>
                <td>
                    <input type="hidden" asp-for="Website" />
                    @Model.Website
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
 
 

Screenshots

Form

ASP.Net Core: Create and download vCard (.VCF) file
 

Downloaded vCard (.VCF) file

ASP.Net Core: Create and download vCard (.VCF) file
 
 

Demo

 
 

Downloads