0

我正在将一个静态网站(使用“Plone”创建)转换为一个 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?

4

2 回答 2

0

You have a bad design because IIS need some how to know what kind of file is, and is use the extension of the image to do that. With out the extension you have an alternative to add some code on global.asax and filter it by the directory as:

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";
    }

}

but I suggest you if this is easy to return the extension on the images, and let IIS handle the ContentType, and the Cache set of the images.

The code may need some tweeks because I do not have test it as it is. Also for this to make work, you must have setup iis to proceed from asp.net the file with out extensions.

于 2012-12-13T07:58:40.130 回答
0

If you can't automate your way out of this situation, then I would suggest writing an HTTP handler that reads an image from disk, writes the bytes to the response and adds the proper mime type to the response.

You can configure a handler in web.config, here's an example:

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

于 2012-12-13T08:03:55.510 回答