本文介绍了在C#中使用超链接创建Outlook .ics文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码假定生成一个.ics文件,该文件中嵌入了链接.它生成.ics,但问题是,链接显示的是html代码而不是Hyperlink.任何想法都可以帮助您.

The code below is suppose to generate an .ics file with a link embedded inside.It generates the .ics but the problem is, the link shows the html code not the Hyperlink. Any ideas can help.

(这是转贴,因为我之前的帖子没有显示完整的代码)谢谢.

(This is a repost since my earlier post didn't show the entire code)Thanks.

public ActionResult ExportToOutlook(Guid id)
{
    var query = new GetActivityByIdQuery();
    var queryResult = query.Execute(id);
    var outlookCalendarFileCreator = new OutlookCalendarFileCreator();
    var contentData = outlookCalendarFileCreator.CreateOutlookCalendarFile(queryResult);

    var fileName = String.Format("Activity.ics", queryResult.ActivityType.Name, queryResult.Date) ;
    return File( contentData,"text/plain", queryResult.Notes + fileName);
}


public class OutlookCalendarFileCreator
{    
    public Byte[] CreateOutlookCalendarFile(ActivityDetailsResponseDto responseDto)
    {
    byte[] contentData;
    HttpRequest request = System.Web.HttpContext.Current.Request;
    string baseUrl = request.Url.GetLeftPart(UriPartial.Authority);
    var hLink = string.Format("{0}/Activities/Details?id={1}", baseUrl, responseDto.Id);
    var sb = new StringBuilder();
    sb.Append(@"X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 3.2//EN"">\n<HTML><BODY>");
    sb.Append(string.Format("<a href='{0}'>{1}</a>", hLink, responseDto.Notes));
    sb.Append("</BODY></HTML>");

    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream))
        {
            writer.AutoFlush = true;
            //HEADER
            writer.WriteLine("BEGIN:VCALENDAR");
            writer.WriteLine("VERSION:1.0");
            writer.WriteLine("BEGIN:VEVENT");

            //BODY
            writer.WriteLine("DTSTART:" + responseDto.Date.ToUniversalTime().ToString("o"));
            writer.WriteLine("DTEND:" + responseDto.Date.AddHours(1).ToUniversalTime().ToString("o"));
            writer.WriteLine("SUMMARY:" + responseDto.ActivityType.Name);
            writer.WriteLine("DESCRIPTION:" + sb.ToString());

            //FOOTER
            writer.WriteLine("END:VEVENT");
            writer.WriteLine("END:VCALENDAR");
            writer.Flush();
            contentData = new byte[(int)memoryStream.Length];
            memoryStream.Position = 0;
            memoryStream.Read(contentData, 0, (int)memoryStream.Length);
            return contentData;
        }
    }
}

推荐答案

同样,在DESCRIPTION后面有:".您需要输入;" -分号与冒号.

Again, you have ":" after DESCRIPTION. You need to have ";" - semicolon vs. colon.

    sb.Append(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 3.2//EN"">\n<HTML><BODY>");
    sb.Append(string.Format("<a href='{0}'>{1}</a>", hLink, responseDto.Notes));
    sb.Append("</BODY></HTML>");
...
    writer.WriteLine("DESCRIPTION;X-ALT-DESC;FMTTYPE=text/html:" + sb.ToString());

这篇关于在C#中使用超链接创建Outlook .ics文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:34