从 ASP.NET MVC3 操作方法内部将控件返回到 IIS



我有这个动作方法,可以即时创建缩略图:

    public ActionResult Thumbnail(int imageId, int width, int height)
    {
        Image image = ImageManager.GetImage(imageId);
        string thumbnailPath;
        if (image.HasThumbnail(width, height))
        {
            thumbnailPath = image.GetThumbnailPath(width, height);
        }
        else
        {
            thumbnailPath = image.CreateThumbnail(width, height);
        }
        /*
        Here, I've done the business of thumbnail creation,
        now since it's only a static resource, I want to let IIS serve it.
        What should I do? Using HttpContext.RewritePaht() doesn't work, as 
        I have to return an ActionResult here.
        */
        return File(image.GetThumbnailPath(width, height), image.MimeType);
    }

调用此方法的 URL 示例为:

/create-thumbnail/300x200/for-image/34

但是,在用这种方法做缩略图创建业务后,我想让IIS提供缩略图。我该怎么办?如何将控件返回到 IIS?

如果缩略图已在文件系统上创建,您可以尝试使用以下操作结果类型之一来返回它。

FileContentResult
FilePathResult
FileStreamResult

..编辑。。用有关输出缓存的更相关答案更新我的回答。

您可能想看看有关 Asp.net 的 Ouput Caching 文章

基本上前提是每次在 MVC 中调用操作时,它都会再次执行整个函数,这对于像缩略图这样简单的东西来说将是一个巨大的性能打击。相反,如果您使用输出缓存装饰您的操作,您可以设置缓存计时器并提高性能。

[OutputCache(Duration = int.MaxValue, VaryByParam = "id;param1;param2")]

VaryByParam 文档

最新更新