自定义视图引擎与自定义操作结果

发布于 2024-09-11 03:50:50 字数 208 浏览 10 评论 0原文

我有一个场景,用户可以选择单击“下载”按钮,我应该创建一个包含历史数据的 csv 文件,然后让用户在本地保存该文件。由于我以前没有这样做过,所以我开始寻找如何执行此操作,并且基本上遇到了自定义视图引擎和自定义操作结果。

我现在的问题是这些的优点/缺点是什么?首选的方法是什么?

CSV 文件基本上只包含标题和数千行数据(最多约 15 列/字段)。所以真的没什么特别的。

I have a scenario where the user have the option to click the button "download" and I should create a csv file, that contains history data, and then let the user save the file locally. As I haven't done this before, I started to look around for how to do this and basically came across both Custom View Engine, and Custom Action Result.

My question now is what is the benefit/disadvantages with these? What is the preferred way to go?

The CSV file, is basically just containing headers, and data (up to about 15 columns/fields) with a few thousand rows. so nothing special really.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-09-18 03:50:50

我可能会选择自定义操作结果(我的示例中的实际序列化是通过 FileHelpers 完成的):

public class CsvResult<T> : ActionResult
{
    public IEnumerable<T> Records { get; private set; }
    public CsvResult(IEnumerable<T> records)
    {
        Records = records;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "text/csv";
        var engine = new FileHelperEngine(typeof(T));
        engine.WriteStream(response.Output, Records);
    }
}

[DelimitedRecord(",")] 
public class Customer 
{
    public int Id { get; set; }
    public string Name { get; set; }         
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var customers = new[]
        {
            new Customer { Id = 1, Name = "customer 1" },
            new Customer { Id = 2, Name = "customer 2" },
        };
        return new CsvResult<Customer>(customers);
    }
}

您可以甚至通过使用扩展方法来美化这个 return 语句(在这种情况下泛型是丑陋且多余的):

public static class ControllerExtensions
{
    public static ActionResult Csv<T>(this Controller controller, IEnumerable<T> records)
    {
        return new CsvResult<T>(records);
    }
}

然后简单地:

public ActionResult Index()
{
    var customers = new[]
    {
        new Customer { Id = 1, Name = "customer 1" },
        new Customer { Id = 2, Name = "customer 2" },
    };
    return this.Csv(customers);
}

I would probably go for a custom action result (the actual serialization in my example is done with FileHelpers):

public class CsvResult<T> : ActionResult
{
    public IEnumerable<T> Records { get; private set; }
    public CsvResult(IEnumerable<T> records)
    {
        Records = records;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "text/csv";
        var engine = new FileHelperEngine(typeof(T));
        engine.WriteStream(response.Output, Records);
    }
}

[DelimitedRecord(",")] 
public class Customer 
{
    public int Id { get; set; }
    public string Name { get; set; }         
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var customers = new[]
        {
            new Customer { Id = 1, Name = "customer 1" },
            new Customer { Id = 2, Name = "customer 2" },
        };
        return new CsvResult<Customer>(customers);
    }
}

You could even prettify this return statement (generics are ugly and superfluous in this case) by using an extension method:

public static class ControllerExtensions
{
    public static ActionResult Csv<T>(this Controller controller, IEnumerable<T> records)
    {
        return new CsvResult<T>(records);
    }
}

and then simply:

public ActionResult Index()
{
    var customers = new[]
    {
        new Customer { Id = 1, Name = "customer 1" },
        new Customer { Id = 2, Name = "customer 2" },
    };
    return this.Csv(customers);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文