私はashxハンドラーを持っています:
<%@ WebHandler Language="C#" Class="Thumbnail" %>
using System;
using System.Web;
public class Thumbnail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string imagePath = context.Request.QueryString["image"];
// split the string on periods and read the last element, this is to ensure we have
// the right ContentType if the file is named something like "image1.jpg.png"
string[] imageArray = imagePath.Split('.');
if (imageArray.Length <= 1)
{
throw new HttpException(404, "Invalid photo name.");
}
else
{
context.Response.ContentType = "image/" + imageArray[imageArray.Length - 1];
context.Response.Write(imagePath);
}
}
public bool IsReusable
{
get { return true; }
}
}
今のところ、このハンドラーが行うのは、画像を取得して返すことだけです。私のaspxページには、次の行があります。
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />
そして、その背後にあるC#コードは次のとおりです。
Image1.ImageUrl = "Thumbnail.ashx?image=../Files/random guid string/test.jpg";
Webページを表示すると、画像が表示されず、HTMLには入力した内容が正確に表示されます。
<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />
なぜこれが機能しないのか誰かに教えてもらえますか?残念ながら、私は昨日ASP.NETを使い始めたばかりで、どのように機能するのかわかりません。可能な限り、説明を簡潔にしてください。ありがとうございます。