微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 使用文件扩展名创建ActionResult的推荐方法

我需要在具有.csv文件类型的ASP.NET MVC应用程序中创建一个ActionResult。

我将向我的营销合作伙伴提供一个“不要打电话”的电子邮件列表,我希望它能够在filetype中添加一个.csv扩展名。然后它会自动在Excel中打开。

http://www.example.com/mailinglist/donotemaillist.csv?password=12334

我已经成功地完成了如下,但我想确保这是绝对最好和推荐的方式做到这一点。

[ActionName("DoNotEmailList.csv")]
    public ContentResult DoNotEmailList(string username,string password)
    {
            return new ContentResult()
            {
                Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),ContentType = "text/csv"
            };
    }

这个Actionmethod会回应上面的链接

我只是想知道是否有任何可能的任何意外的冲突,有这样的文件扩展名与任何不同版本的IIS,任何种类的ISAPI过滤器,或任何我现在不能想到的东西。

我需要100%肯定,因为我会把这个提供给外部合作伙伴,不想再想改变主意了。我真的看不到任何问题,但也可能是某些晦涩的东西 – 或者另外一个“MVC”就像这样做。

解决方法

在这种情况下,我认为你的回应必须包含“Content-disposition”头。创建自定义ActionResult,如下所示:
public class MyCsvResult : ActionResult {

    public string Content {
        get;
        set;
    }

    public Encoding ContentEncoding {
        get;
        set;
    }

    public string Name {
        get;
        set;
    }

    public override void ExecuteResult(ControllerContext context) {
        if (context == null) {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        response.ContentType = "text/csv";

        if (ContentEncoding != null) {
            response.ContentEncoding = ContentEncoding;
        }

        var fileName = "file.csv";

        if(!String.IsNullOrEmpty(Name)) {
            fileName = Name.Contains('.') ? Name : Name + ".csv";
        }

        response.addheader("Content-disposition",String.Format("attachment; filename={0}",fileName));

        if (Content != null) {
            response.Write(Content);
        }
    }
}

并在您的Action中使用它而不是ContentResult:

return new MyCsvResult {
    Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
    /* Optional
     *,ContentEncoding = ""
     *,Name = "DoNotEmailList.csv"
     */
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: ' Style="position: relative" AppendDataBoundItems="True"> (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke '*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you