I have a .net core web API, which has an endpoint returns iCal for the users.
[HttpGet("Download/iCal")]
public ActionResult GetiCal()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:1.0");
sb.AppendLine("CALSCALE:GREGORIAN");
sb.AppendLine("BEGIN:VEVENT");
sb.AppendLine("DTSTART:" + date.ToString("yyyyMMddTHH:mm:ss"));
sb.AppendLine("DTEND:" + date.ToString("yyyyMMddTHH:mm:ss"));
sb.AppendLine("SUMMARY:" + summary + "");
sb.AppendLine("LOCATION:" + address + "");
sb.AppendLine("DESCRIPTION:" + Description + "");
sb.AppendLine("PRIORITY:3");
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
string CalendarItem = sb.ToString();
byte[] calendarBytes = System.Text.Encoding.UTF8.GetBytes(CalendarItem);
return FileContent(calendarBytes, "text/calendar", "restcal.ics");
}
public virtual FileContentResult FileContent(byte[] fileContents, string contentType, string name)
=> new FileContentResult(fileContents, contentType) { FileDownloadName = name };
I'm accessing this endpoint from another MVC web application like below.
public async Task<FileStreamResult> GetCalendar()
{
var response = await m_HttpClient.GetAsync(address + "/Download/iCal/");
List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>();
formatters.Add(new TextMediaTypeFormatter());
return await response.Content.ReadAsAsync<FileStreamResult>(formatters);
}
public class TextMediaTypeFormatter : MediaTypeFormatter
{
public TextMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/calendar"));
}
public override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override bool CanWriteType(Type type)
{
return false;
}
}
I have added formatters and TextMediaTypeFormatter class according to one of the answers given for this question. But that answer is for "text/html". I thought it will work for "text/calendar" as well. But I still do get this "No MediaTypeFormatter is available" Error. How can I solve this?