1

我只想创建一个包含 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: 
4

2 回答 2

1

我已经测试并强烈推荐为 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
于 2014-01-16T05:04:24.140 回答
0

嗯,我不明白你想用这条线实现什么

CreateFile("John", "Dupont");

在您的索引操作中...

你可以试试吗,在你看来

@Html.ActionLink("Download your VCF", "CreateFile", "Home", new{firstName="John", lastName="Dupont"}, null)

因为在您看来,您没有将任何论据传递给您的行动……所以您什么也得不到!

于 2013-08-22T14:40:14.200 回答