首页 > 解决方案 > c#为Outlook生成ics文件

问题描述

我创建了一个 ashx 文件,该文件生成一个用于 Outlook 的 ics 文件。我的 1 个日历代码运行良好。我现在添加了这样一个事实,即 ashx 页面接受一个 guid,该 guid 用于检查数据库应该生成哪个日历。

因此,人们可以通过在其末尾添加另一个 guid,从单个 ashx 文件中添加多个日历。现在的问题是第一个日历生成正常,但第二个失败。我看到它在 ashx 文件中达到了我的断点,它生成的一切都很好,但 Outlook 似乎没有收到它。我假设 Outlook 阻止了 ics 文件的生成,因为对他来说它是相同链接的两倍。

然后我在我的网络服务器上实现了 url 重定向,使其看起来像是多个不同的 url 到 Outlook。我再次看到它两次命中断点,但只生成了 1 个日历,另一个给我错误“无法验证或将日历添加到 Outlook,......”并且链接有效。

因为它来自同一个 ashx 文件,outlook 是否仍然阻止刷新多个日历?

public class CalendarService : IHttpHandler
    {
        private readonly OutlookCalendarDomainLogic _domainLogic = InitializeDomainLogic();

        private static OutlookCalendarDomainLogic InitializeDomainLogic()
        {
            var repository = new OutlookCalendarRepository();
            OutlookCalendarDomainLogic logic = new OutlookCalendarDomainLogic(repository);
            return logic;
        }

        public void ProcessRequest(HttpContext context)
        {
            if (!Guid.TryParse(context.Request["Guid"], out Guid guid))
            {
                return;
            }

            var co = _domainLogic.GetOutlookCalendarByGuid(guid);

         ..... Event generation code
//send the calendar item to the browser
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "text/calendar";
            HttpContext.Current.Response.AddHeader("content-length", CalendarItem.Length.ToString());
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=\"" + CalendarName + ".ics\"");
            HttpContext.Current.Response.Write(CalendarItem);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
}

有任何想法吗?

标签: c#asp.netoutlookashx

解决方案


推荐阅读