Link 剃刀视图的文件内容结果?仅通过 action/url 获取文本转储

Link filecontentresult from a razor view? Only getting the text dump via action/url

使用 ical.net nuget 包,我想为显示在自定义列表中的事件整理一个简单的 ical 下载 link。

我已经尝试在视图中使用操作link 和Html.Beginform,但两者都给出了相同的结果。只是一个 404,带有 url/controller/action?start="ical text contents"。我需要通过其他方式调用它来获取实际文件名吗?

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

我能够使用网络 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;
        }
   }
}

了解了如何放松并意识到路由存在问题。解决了这个问题,我#blessed 拥有 ics 文件和 Whosebug 知识。