#需求:
前端通过<img>的src向服务端请求图片信息,如果不存在想要的图片,那么就返回一张默认路径下的图片;
#实现:
<img class="related_resources" src="PictureHandle.ashx?id=42&type=thumbnail">
public void ProcessRequest (HttpContext context) {
string type = context.Request.QueryString["type"] != null ? context.Request.QueryString["type"].ToString() : "";
string url = ConfigurationManager.AppSettings["RestUrl"] + "Resource";
if (type == "thumbnail")
{
context.Response.ContentType = "text/plain";
url = url + "/thumbnail?ID=" + id;
try
{
bytes = new WebClient().DownloadData(url);
context.Response.BinaryWrite(bytes);
}
catch(Exception)
{
context.Response.Redirect("image/default.jpg"); }
}
}