我有一个 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";
当我查看网页时,图像没有显示,HTML 显示了我输入的内容:
<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />
有人可以告诉我为什么这不起作用吗?不幸的是,我昨天才开始使用 ASP.NET,我不知道它是如何工作的,所以请尽可能保持简单的解释,谢谢。