0

ImageHandlerを使用して画像を出力するasp.netWebフォームアプリケーションがあります。基本的にリーチングを防ぐためだけでなく、別のサーバーから画像ファイルを取得するためでもあります。

ProcessRequestの実装は次のとおりです。

public void ProcessRequest(HttpContext ctx)
    {
        HttpRequest req = ctx.Request;
        string path = req.PhysicalPath.ToLower();
        string extension = Path.GetExtension(path);

        if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)
        {
            if (CultureInfo.InvariantCulture.CompareInfo.Compare(req.Url.Host, req.UrlReferrer.Host, CompareOptions.IgnoreCase) != 0)
            {
                path = ctx.Server.MapPath("~/images/noimage.jpg");
            }
        } 

        // Rewrite path if not in production
        if (imagePath != null)
        {
            if (path.Length > path.IndexOf("\\images\\", StringComparison.Ordinal) + 7)
            {
                string end = path.Substring(path.IndexOf("\\images\\", StringComparison.Ordinal) + 7);
                path = string.Concat(imagePath, end).Replace("\\", "/");
            }
        }

        string contentType;

        switch (extension)
        {
            case ".gif":
                contentType = "image/gif";
                break;
            case ".jpg":
                contentType = "image/jpeg";
                break;
            case ".png":
                contentType = "image/png";
                break;
            default:
                throw new NotSupportedException("Unrecognized image type.");
        }

        if (!File.Exists(path))
        {
            ctx.Response.Status = "Image not found";
            ctx.Response.StatusCode = 404;
        }
        else
        {
            ctx.Response.StatusCode = 200;
            ctx.Response.ContentType = contentType;
            ctx.Response.WriteFile(path);
        }
    }

パスをファイルパスではなくURLに書き換えたいため、上記のコードは失敗します。書き直したいのは、実際の画像ファイルが別のサーバー上にあり、UNCパス経由でアクセスできないためです。私は何を間違っているのですか、そしてこれを達成することはまったく可能ですか?

乾杯

4

1 に答える 1

0

このハンドラーを呼び出しているときは、パスを書き換えようとしているので、パスを書き換えることはできなくなります。

書き換えパスは、IISとasp.netに対して、たとえばへの呼び出しhttp://www.url.com/one/page1.aspxhttp://www.url.com/someotherpage.aspx?id=one

データの最終処理を行う準備ができていないので、ファイルを読み取ってブラウザに送信する必要があります。たとえば、次のようになります。

 public void ProcessRequest(HttpContext context)
    {
      // your first code
      // ...
       if (!File.Exists(path))
        {
            ctx.Response.Status = "Image not found";
            ctx.Response.StatusCode = 404;
        }
        else
        {
            // load here the image 
            ....
            // and send it to browser
            ctx.Response.OutputStream.Write(imageData, 0, imageData.Length);
        }
    }
于 2013-01-07T15:24:24.003 に答える