Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送响应时发生的事件_C#_Asp.net Mvc 4_Asp.net Web Api - Fatal编程技术网

C# 发送响应时发生的事件

C# 发送响应时发生的事件,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,我有一个控制器类,它继承自ApiController,并处理来自客户端的HTTP请求 其中一个操作在服务器上生成一个文件,然后将其发送到客户端 我正试图弄清楚在响应完成后如何清理本地文件 理想情况下,这将通过一个事件来完成,该事件在向客户端发送响应后触发 有这样的事件吗?或者我想要达到的目标是否有一个标准模式 [HttpGet] public HttpResponseMessage GetArchive(Guid id, string outputTypes) { // // G

我有一个控制器类,它继承自
ApiController
,并处理来自客户端的HTTP请求

其中一个操作在服务器上生成一个文件,然后将其发送到客户端

我正试图弄清楚在响应完成后如何清理本地文件

理想情况下,这将通过一个事件来完成,该事件在向客户端发送响应后触发

有这样的事件吗?或者我想要达到的目标是否有一个标准模式

[HttpGet]
public HttpResponseMessage GetArchive(Guid id, string outputTypes)
{
    //
    // Generate the local file
    //
    var zipPath = GenerateArchive( id, outputTypes );

    //
    // Send the file to the client using the response
    //
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new FileStream(zipPath, FileMode.Open);
    response.Content = new StreamContent(stream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
    response.Content.Headers.ContentLength = new FileInfo(zipPath).Length;
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = Path.GetFileName(zipPath)
    };

    return response;
}
看一看事件-您可以向方法中添加一个自定义筛选器并在那里处理事件

public class CustomActionFilterAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
         ///filterContext should contain the id you will need to clear up the file.
    }
}
Global.asax中的事件也可能是一个选项

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}
看一看事件-您可以向方法中添加一个自定义筛选器并在那里处理事件

public class CustomActionFilterAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
         ///filterContext should contain the id you will need to clear up the file.
    }
}
Global.asax中的事件也可能是一个选项

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}

就不能在行动结束时发射吗?或者您是否允许他们直接访问生成的文件?不,该操作返回响应,我假设该响应随后由基类异步发送到客户端。不能在操作结束时触发它吗?或者您是否允许他们直接访问生成的文件?否,该操作返回响应,我假设该响应随后由基类异步发送给客户端。我实现了一个解决方案,使用流子类在释放流时删除该文件。然而,我也得到了一个处理
EndRequest
事件的解决方案。我实现了一个解决方案,使用流子类在释放流时删除文件。然而,我也得到了一个处理
EndRequest
事件的解决方案。