2012-11-24 82 views
2

我如何使StaticFile處理程序只提供實際存在的文件,並使其他人轉到asp.net mvc管道或自定義http處理程序。服務靜態文件,如果它存在,否則創建它

1) GET /images/file.jpg 
    exists => serve it (StaticFile handler/as efficiently as possible) 

2) GET /images/file_640x480.jpg 
    1. request (doesn't exist) 
    * load file.jpg 
    * resize 
    * save as file_640x480.jpg 
    => serve from memory 

    following requests 
    => should use 1) because the file is now there 

這是瑣碎<modules runAllManagedModulesForAllRequests="true" /> 但我想這更多的是一種解決方法不能真正解決問題的。

回答

0

我沒有嘗試過在這些線路上的代碼,但事情應該工作

public FilePathResult GetFile(string name) 
{ 
    FileInfo info = new FileInfo(name); 
    if (!info.Exists) 
    { 
     //Code to load the original file, resize and save would go here 
    } 

    return File(name, "text/plain"); 
} 
相關問題