我在我的Asp.Net MVC項目中使用HttpHandler。我有另一個MVC API項目返回圖像作爲迴應。使用HttpWebRequest我可以調用API,代碼中沒有錯誤,但我無法查看頁面中的圖像。獲取圖像作爲HttpWebRequest在HttpHandler中的響應
我的代碼:
的HttpHandler代碼:
var currentResponse = HttpContext.Current.Response;
string URL = "http://localhost:50417/API/GetThumbnail";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.Timeout = 30000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamr = new StreamReader(response.GetResponseStream());
currentResponse.Write(streamr.ReadToEnd());
RouteConfig.cs
routes.Add(new Route("Thumbnail/getImage", new ThumbnailImageRouteHandler()));
index.csHtml
<img src="/Thumbnail/getImage" />
您是否嘗試過指定contenttype?如果您在瀏覽器中打開網址,您是否可以下載/查看圖像? – Fixation
@Fixation是我可以下載圖片,如果我在瀏覽器中打開網址,我也試過contenttype – Jigarb1992