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パス経由でアクセスできないためです。私は何を間違っているのですか、そしてこれを達成することはまったく可能ですか?
乾杯