ImageResizer没有从webapi 2控制器触摸文件

时间:2015-05-11 23:58:18

标签: c# asp.net-web-api imageresizer

我已经阅读了这篇文章:

ImageResizer is not resizing images served by WebAPI

我已经在这里阅读了关于WebAPI和ImageResizer的相关常见问题解答:

http://imageresizing.net/docs/best-practices

我仍然不明白我需要做些什么来制作路线所服务的文件:

config.Routes.MapHttpRoute
         ("API Vimeo Thumbnail", "rpc/{controller}/{action}/{vimeoId}.jpg",
             new
             {

             });

并由此控制器处理:

public class VimeoController : ApiController
    {
        [HttpGet]
        [ActionName("Thumbnail")]
        public HttpResponseMessage Thumbnail(string vimeoId = null, int? width = 1280, int? height = 720)
        {
            string url = String.Format("https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/{0}&width={1}&height={2}", vimeoId, width,
                height);
            var endpointRequest = (HttpWebRequest) WebRequest.Create(url);
            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
            //var endpointResponse = (HttpWebResponse) endpointRequest.GetResponse();
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            try
            {
                using (WebResponse webResponse = endpointRequest.GetResponse())
                {
                    using (Stream webStream = webResponse.GetResponseStream())
                    {
                        using (var responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            var reader = new JsonTextReader(new StringReader(response));
                            while (reader.Read())
                            {
                                if (reader.Value != null && reader.Value.Equals("thumbnail_url"))
                                {
                                    reader.Read();
                                    string downloadUrl = reader.Value.ToString();
                                    var wc = new WebClient();
                                    using (var stream = new MemoryStream(wc.DownloadData(downloadUrl)))
                                    {
                                        result.Content = new ByteArrayContent(stream.ToArray());
                                        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                                    }
                                }
                            }
                            responseReader.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
                Console.ReadLine();
            }
            return result;
        }
    }

让我感到困惑的是ImageResizer如何在我的/ Images目录中获取文件,而不是从此处理程序中获取文件。显然,我不了解管道以及如何进入管道。

1 个答案:

答案 0 :(得分:0)

ImageResizer无法正常工作;它在HandleRequest阶段之前运行,以便利用IIS静态文件服务。如果要提供自定义行为,则必须子类化BlobProviderBase或实现IVirtualImageProvider。如果您只需要HTTP请求,则可以尝试使用RemoteReader插件(如果要更改语法,则重写URL)。

有关详细信息,请参阅integrating with a custom data store