2015-06-26 47 views
1

想象一下,ASP.NET MVC Web應用程序允許用戶瀏覽服務器上某些VCS存儲庫的內容。現在,這個存儲庫,其中包含*.cshtml文件,我想「按原樣」進行渲染。ASP.NET MVC:按原樣服務.cshtml文件

換句話說,當用戶導航到http://example.com/browse/directory/subdirectory/main.cs時,將顯示main.cs文件的「原始」內容 - 並且這已經正常工作。但是,訪問http://example.com/browse/directory/subdirectory/main.cshtml會導致ASP.NET運行時內部存在HttpException。

我試圖從/browse取出BlockViewHandler,並明確允許<requestFiltering>*.cshtml文件 - 但無濟於事:

<security> 
    <requestFiltering> 
     <fileExtensions> 
     <remove fileExtension=".cshtml" /> 
     <remove fileExtension=".vbhtml" /> 

     <add fileExtension=".cshtml" allowed="true" /> 
     <add fileExtension=".vbhtml" allowed="true" /> 
     </fileExtensions> 
    </requestFiltering> 
    </security> 
</system.webServer> 

回答

1

嘗試直接從文件中讀取它系統本身,所以它繞過剃刀。例如:

string filePath = Server.MapPath("~/directory/subdirectory/main.cshtml"); 
string rawTemplate = System.IO.File.ReadAllText(filePath); 

然後將其填充到ViewModel中或直接寫入響應?你可能需要Html.Raw()來顯示它,所以如果用戶可以編輯它,你可能想從XSS東西中清理模板。

+0

這些.cshtml文件無論如何都不在磁盤上 - 它們位於VCS存儲庫中,我可以通過它編程獲取它們。這裏的罪魁禍首是以「.cshtml」結尾的URL,ASP.NET似乎在扼殺。 –