title: How to flush response in ActionFilter of asp.net core?
body: <p>I am trying to delete a file that is dynamically generated at my controller using an ActionFilter.</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>As mentioned in the code comment, since I am not able to flush the response and then delete the file I get <code>IOException: The process cannot access the file ... because it is being used by another process</code> exception.</p>
  
  <p>What is a sure way to delete the file in the action filter after the user has finished downloading it?</p>