【问题标题】:How to flush response in ActionFilter of asp.net core?如何在 asp.net core 的 ActionFilter 中刷新响应?
【发布时间】:2016-09-22 08:45:52
【问题描述】:

我正在尝试删除在我的控制器上使用 ActionFilter 动态生成的文件。

public class DeleteFileAttribute : ActionFilterAttribute {
    public override void OnActionExecuted(ActionExecutedContext context) {
        /* MVC 4 code which worked just fine
        context.HttpContext.Response.Flush();

        var filePathResult = context.Result as FilePathResult;

        if(filePathResult!=null){
            File.Delete(filePathResult.FileName);
        }
        End of MVC 4 code */

        // I am not able to flush response as context.HttpContext.Response does not have a Flush method
        var vb = (context.Controller as Controller).ViewBag;
        string fileToDelete = vb.FileToDelete;

        if(File.Exists(fileToDelete)) {
            // ERROR: Process cannot access the file ... because it is being used by another process
            File.Delete(fileToDelete);
        }
    }
}

如代码注释中所述,由于我无法刷新响应然后删除文件,因此我得到了IOException: The process cannot access the file ... because it is being used by another process 异常。

在用户完成下载后,在操作过滤器中删除文件的可靠方法是什么?

【问题讨论】:

    标签: c# asp.net .net asp.net-core .net-core


    【解决方案1】:

    在操作过滤器中使用OnResultExecuted 事件而不是OnActionExecuted 解决了这个问题。不需要刷新响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2018-09-03
      • 1970-01-01
      相关资源
      最近更新 更多