我已经测试并强烈推荐为 VCARD 创建一个特定的操作结果,请参阅这篇文章
http://dotnet.dzone.com/news/creating-vcard-action-result
控制器代码
public virtual vCardActionResult VCard(int id)
{
//
var requestedPerson = Data.Persons.GetById(id);
vCard personCard = new vCard();
personCard.FirstName = requestedPerson.Firstname;
personCard.LastName = requestedPerson.Lastname;
personCard.Organization = requestedPerson.Company;
personCard.JobTitle = requestedPerson.Firstname;
personCard.Phone = requestedPerson.Phone1;
personCard.Mobile = requestedPerson.Mobile;
personCard.Country= requestedPerson.Site.Name;
personCard.Address = requestedPerson.Address1 ;
personCard.Email = requestedPerson.Email1;
//The template file laid on the root vcard.txt
return new vCardActionResult(personCard);
}
自定义操作结果
public class vCardActionResult : ActionResult
{
private vCard _card;
protected vCardActionResult() { }
public vCardActionResult(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.Body();
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);
}
}
这就是你需要的课程
public class vCard
{
public string FirstName = "";
public string LastName ="";
public string Organization = "";
public string JobTitle = "";
public string Address = "";
public string Phone = "";
public string Mobile = "";
public string Email = "";
public string Country = "";
public string Website = "";
public string Body()
{
var cardTemplate = "";
using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(@"~/vcard.txt")))
{
cardTemplate = sr.ReadToEnd();
}
cardTemplate = cardTemplate
.Replace("{{firstname}}", FirstName)
.Replace("{{lastname}}", LastName)
.Replace("{{organization}}", Organization)
.Replace("{{jobtitle}}", JobTitle)
.Replace("{{workphone}}", Phone)
.Replace("{{mobilephone}}", Mobile)
.Replace("{{country}}", Country)
.Replace("{{address}}", Address)
.Replace("{{email}}", Email);
return cardTemplate;
}
}
这是 vcard 模板文本文件 vcard.txt 以防您也需要它
BEGIN:VCARD
VERSION:2.1
N;LANGUAGE=en-au:{{lastname}};{{firstname}}
FN:{{firstname}} {{lastname}}
ORG:{{organization}}
TITLE:{{jobtitle}}
TEL;WORK;VOICE:{{workphone}}
TEL;CELL;VOICE:{{mobilephone}}
ADR;WORK;PREF:;;;;{{country}};;{{country}}
LABEL;WORK;PREF:{{address}}
EMAIL;PREF;INTERNET:{{email}}
REV:20140116T013303Z
END:VCARD