Создайте класс и унаследуйте форму actionfilterattribute и переопределите метод OnResultExecuted
Public Class DeleteFileAttribute
Inherits ActionFilterAttribute
Public Overrides Sub OnResultExecuted(ByVal filterContext As ResultExecutedContext)
Dim path As String = filterContext.HttpContext.Server.MapPath("~/LocalizeData")
Dim filename As String = DirectCast(filterContext.Result, CSVResult).FileName 'filterContext.RouteData.Values("Path").ToString()
If File.Exists(System.IO.Path.Combine(path, filename)) Then
File.Delete(System.IO.Path.Combine(path, filename))
End If
End Sub
End Class
и применить атрибут удаления к функции
например
<DeleteFile()>
Public Function GetCSVFile(ByVal listSeperator As String) As CSVResult
Dim result As Boolean
Dim Service As New Service()
Dim types As IList(Of ProductType) = Service.GetAllWithAllLocalizedNames(New ListOptions(Name", OrderByDirection.Ascending, 1, -1))
Dim path As String, colname As String
colname = Settings.GetLanguage()
path = Common.GetLocalizeDataPath()
Dim seprator As String = Common.GetListSepratorForCSV(listSeperator)
Dim cs As New CSVGenerator(path, colname, seprator)
result = cs.CreateCSVFile(types)
If result Then
Return New CSVResult With {.FileName = "Sample.csv", .Path = "~/Mydir/Sample.csv"}
Else
Return New CSVResult With {.FileName = "Error.csv", .Path = "~/Mydir/Error.csv"}
End If
End Function
и еще одна проблема с результатом выполнения, пожалуйста, промойте ответ и закройте его
context.HttpContext.Response.Flush()
context.HttpContext.Response.End()