2012-12-13 54 views
0

我正在将一个静态网站(使用“Plone”创建)转换为ASP.NET项目,并且它引用图像的方式很奇怪,并且在ASP.NET中不起作用。在ASP.NET中处理无扩展图像

的结构是这样的:/images/image.png/image_thumb/images/image.png/image_tile

image.png是一个文件夹,image_thumbimage_tile的文件。我需要做什么手动添加扩展到每个文件并引用它?

这里是一个图像是如何目前引用(在旧的静态项目,但不是在ASP.NET作品):<img alt="Shallow Tiger" class="image-inline" height="300" src="/images/rm/ue147.JPG/image_preview" width="400" />

有什么事情,我需要在Global.asax中做什么?或web.config?或IIS?

+0

找到一种方法来自动化手动过程?使用[免费文件重命名程序(https://www.google.com/search?q=free+file+rename+program+for+windows)重命名的图像,并使用正则表达式搜索和可视更换Studio来更改代码中的'src'属性。 –

+0

另外,它在ASP.NET中不起作用?你的意思是IIS?旧网站是否在Apache上运行?你能否在你的问题中澄清一下? –

回答

0

你有一个糟糕的设计,因为IIS需要一些如何知道什么样的文件是,并使用图像的扩展来做到这一点。随着走出延伸你有一个替代加上global.asax一些代码,由目录筛选它:

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    HttpApplication app = (HttpApplication)sender; 
    string cTheFile = HttpContext.Current.Request.Path; 
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile); 

    // if this is a file inside the directory images 
    // and did not have extention 
    if ( cTheFile.Contains("/images/", StringComparison.InvariantCultureIgnoreCase) 
     && sExtentionOfThisFile.Equals(".", StringComparison.InvariantCultureIgnoreCase) 
    ) 
    { 
     // then send the Content type of a jpeg (if its jpeg) 
     app.Response.ContentType = "image/jpeg"; 
    } 

} 

但我建议你,如果这是很容易恢复的图像的扩展,并让IIS处理ContentType和图像的缓存集合。

代码可能需要一些tweeks,因为我没有测试它,因为它是。 此外,为了使工作,你必须设置iis从asp.net文件扩展名。

0

如果你不能自动化你的出路,那么我会建议你写一个HTTP handler,它从磁盘读取图像,将字节写入响应,并将适当的MIME类型添加到响应中。

您可以configure a handler在web.config中,这里有一个例子:

<system.web> 
    <httpHandlers> 
    <add verb="GET" path="images/*" type="ExtensionLesssImageHandler, WebApplication1" /> 
    </httpHandlers>