使用ical.net nuget包,我想为显示在自定义列表中的事件建立一个简单的ical下载链接。
我尝试在视图中使用动作链接和Html.Beginform,但是两者都给出相同的结果。只是一个404,带有url / controller / action?start =“ ical文本内容”。我需要调用这种方法来获取实际的文件名吗?
[HttpPost]
public FileContentResult DownloadiCal(DateTime start, DateTime end, string name, string location, string description)
{
var e = new CalendarEvent
{
Start = new CalDateTime(start),
End = new CalDateTime(end),
Location = location,
Description = description
};
var calendar = new Calendar();
calendar.Events.Add(e);
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
byte[] calendarBytes = System.Text.Encoding.UTF8.GetBytes(serializedCalendar); //iCal is the calendar string
return File(calendarBytes, "text/calendar", "event.ics");
}
答案 0 :(得分:1)
我能够使用Web api控制器使其工作。
using System;
using Ical.Net;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
using System.Web.Http;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
namespace DEMO.API
{
public class CalendarsController : ApiController
{
[AllowAnonymous]
[HttpPost]
[Route("api/calendar")]
public IHttpActionResult Get()
{
IHttpActionResult response;
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
var e = new CalendarEvent
{
Start = new CalDateTime(DateTime.Now),
End = new CalDateTime(DateTime.Now.AddHours(1)),
Location = "Eric's Cube",
Description = "Chillin at Eric's cube. who you with? me and my peeps why you bring 4 of your friiiiiieeeends."
};
var calendar = new Calendar();
calendar.Events.Add(e);
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
byte[] calendarBytes = System.Text.Encoding.UTF8.GetBytes(serializedCalendar); //iCal is the calendar string
responseMessage.Content = new ByteArrayContent(calendarBytes);
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/calendar");
response = ResponseMessage(responseMessage);
return response;
}
}
}
答案 1 :(得分:1)
了解了如何冷却并意识到路由存在问题。解决了这个问题,我很高兴拥有ics文件和Stackoverflow知识。