我只想创建一个包含MVC ASP .NET中的一些参数的文本文件。
使用此代码,我可以阅读我的文字,但它不接受我的参数。
public FileStreamResult CreateFile(string firstName, string lastName)
{
var data = "Hello,\r\n" + "Firstname: " + firstName + "\r\nLastName: " + lastName;
var byteArray = Encoding.ASCII.GetBytes(data);
var stream = new MemoryStream(byteArray);
return File(stream, "text/plain", "VCard.vcf");
}
在我的控制器中:
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
CreateFile("John", "Dupont");
return View();
}
在我看来:
@Html.ActionLink("Download your VCF", "CreateFile", "Home")
输出:
Hello,
Firstname:
LastName:
答案 0 :(得分:1)
我已经测试并强烈推荐为VCARD创建特定的Action结果 参考这篇文章 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);
}
自定义Actionresult
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
答案 1 :(得分:0)
好吧,我不明白你想用线路实现什么
CreateFile("John", "Dupont");
在你的索引行动......
你可以尝试,在你看来
@Html.ActionLink("Download your VCF", "CreateFile", "Home", new{firstName="John", lastName="Dupont"}, null)
因为你没有在你的视图中将任何参数传递给你的动作......所以你什么都没得到!