この記事http://dotnet.dzone.com/news/creating-vcard-action-resultを参照して、VCARD の特定のアクション結果を作成することをテストし、強くお勧めし
ます
コントローラーコード
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