【.NET】asp.net Redirect 图片路径

#需求:

  前端通过<img>的src向服务端请求图片信息,如果不存在想要的图片,那么就返回一张默认路径下的图片;

#实现:

1 <img class="related_resources" src="PictureHandle.ashx?id=42&type=thumbnail">
 1 public void ProcessRequest (HttpContext context) {
 2   string type = context.Request.QueryString["type"] != null ? context.Request.QueryString["type"].ToString() : "";
 3   string url = ConfigurationManager.AppSettings["RestUrl"] + "Resource";
 4   if (type == "thumbnail")
 5   {
 6             context.Response.ContentType = "text/plain";
 7             url = url + "/thumbnail?ID=" + id;
 8             try
 9             {
10                 bytes = new WebClient().DownloadData(url);
11                 context.Response.BinaryWrite(bytes);
12             }
13             catch(Exception)
14             {
15                  context.Response.Redirect("image/default.jpg");
16   
17             }
18     }
19 }
posted @ 2018-09-07 14:50  willingtolove  阅读(378)  评论(0编辑  收藏  举报