【问题标题】:Create a text with parameters - MVC .NET创建带有参数的文本 - MVC .NET
【发布时间】:2013-08-22 14:32:35
【问题描述】:

我只想在 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: 

【问题讨论】:

  • 您是否尝试将“数据”设置为字符串?
  • 是的,但问题是一样的:'(

标签: asp.net-mvc filestream outputstream


【解决方案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

【讨论】:

    【解决方案2】:

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

    CreateFile("John", "Dupont");
    

    在您的索引操作中...

    你可以试试吗,在你看来

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

    因为您在视图中没有将任何参数传递给您的操作...所以您什么也得不到!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 2018-05-25
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2020-05-31
      相关资源
      最近更新 更多