从剃刀视图链接filecontentresult?只获得通过动作/ URL文本转储

问题描述 投票:1回答:2

使用ical.net NuGet包,我想放在一起简单的iCal下载链接,在自定义列表中显示的事件。

我已经使用ActionLink的,并且Html.Beginform在视图中尝试过,但都给予同样的结果。就在404与URL /控制器/动作?开始=“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");
    }
c# asp.net-mvc razor icalendar actionresult
2个回答
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
投票

学会了如何放松,并意识到有一个与路由的问题。解决了,我用的IC文件和知识#1 #blessed。

© www.soinside.com 2019 - 2024. All rights reserved.