C# 从HttpHandler中的HttpWebRequest获取图像作为响应

C# 从HttpHandler中的HttpWebRequest获取图像作为响应,c#,asp.net-mvc,httphandler,httpwebresponse,C#,Asp.net Mvc,Httphandler,Httpwebresponse,我正在Asp.NETMVC项目中使用HttpHandler。我还有另一个MVCAPI项目,它返回图像作为响应。使用HttpWebRequest,我可以调用API,代码中没有错误,但我无法查看页面中的图像 我的代码: HttpHandler代码: var currentResponse = HttpContext.Current.Response; string URL = "http://localhost:50417/API/GetThumbnail";

我正在Asp.NETMVC项目中使用HttpHandler。我还有另一个MVCAPI项目,它返回图像作为响应。使用HttpWebRequest,我可以调用API,代码中没有错误,但我无法查看页面中的图像

我的代码:

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
并将响应流复制到输出,则此操作有效,如下所示:

response.GetResponseStream().CopyTo(currentResponse.OutputStream);
currentResponse.ContentType = response.ContentType;

您是否尝试指定contenttype?如果在浏览器中打开URL,您可以下载/查看图像吗?@是的,如果在浏览器中打开URL,我可以下载图像,我还尝试了contenttypeIt。当我用上面的两行替换示例中的最后两行时,它对我起到了作用。您在Visual Studio或web浏览器控制台中是否遇到任何错误?哎呀!抱歉,我再次检查了它,并且我给出的路径是错误的。它起作用了@Johnny谢谢:)