0

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?

enter image description here

user2837480
  • 349
  • 2
  • 18
  • Which version of the Asp.net Core are you using? I tested your API code with the [custom formatters sample](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/web-api/advanced/custom-formatters/samples), everything works well on my side, check [this screenshot](https://i.stack.imgur.com/vN8p0.gif). So, you could refer the official [custom formatters sample](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/web-api/advanced/custom-formatters/samples) to modify your code. – Zhi Lv Jun 14 '21 at 05:55
  • @ZhiLv Thanks for the reply. It's .net core 2.2 project. The application which access this API is MVC 4.7. Actually these two are two different solutions. One is for backend and other one is for frontend. If I call the API directly through postman its working. But I'm calling the Core API inside my MVC (4.7 framework) application and I'm getting this error when mapping API response to the model. – user2837480 Jun 15 '21 at 06:20

0 Answers0