Ссылка filecontentresult с точки зрения бритвы?Получение только дампа текста через action / url - PullRequest
0 голосов
/ 06 февраля 2019

Используя пакет ical.net nuget, я хотел собрать простую ссылку ical для загрузки событий, отображаемых в пользовательском списке.

Я пытался использовать actionlink и Html.Beginform в представлении, но оба дают одинаковый результат.Просто 404, с URL / контроллером / действием? Start = "текстовое содержимое".Есть ли другой способ, которым я должен вызывать это, чтобы получить фактическое имя файла?

[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");
    }

Ответы [ 2 ]

0 голосов
/ 08 февраля 2019

Научился охлаждать и понял, что есть проблема с маршрутизацией.Решил это, и я #blesed с файлами ics и знанием Stackoverflow.

0 голосов
/ 07 февраля 2019

Мне удалось заставить его работать с помощью контроллера веб-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;
        }
   }
}
...