ASP.NET(MVC)提供图片 [英] ASP.NET (MVC) Serving images

查看:112
本文介绍了ASP.NET(MVC)提供图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个MVC 3应用程序(虽然同样适用于其他技术,如ASP.NET表单),并只是想知道,如果它是可行的(性能明智),以满足从code图像,而不是使用直接虚拟路径(如往常一样)。

的想法是,我提高文件投放到的常用方法:


  1. 申请安全检查

  2. 基于路由值提供文件服务的标准化方法

  3. 返回修改图像(如果需要)如不同dimentions(OK这只能尽量少用,所以不要这与上面的性能问题)。

  4. 允许访问资源之前执行业务逻辑

我知道该怎么做,但我不知道是否我应该这样做。


  1. 什么是性能问题(如果有的话)

  2. 是否奇怪的事情发生如只有负载依次拍摄(也许这就是HTML是怎么做的目前我不知道 - 在这里暴露我的无知)。

  3. 其他任何你能想到的。

希望这一切才有意义!

谢谢,

更新

确定 - 让我们得到具体的:

什么是使用这类方法的使用内存流服务于MVC 3的所有图像的性能影响?注:图片的URL会GenericFetchImage / image1的(并且只是简单 - 我所有的图像是JPEG文件)。

 公共FileStreamResult GenericFetchImage(字符串RouteValueRefToImage)
{
    //创建一个新的内存流对象
    MemoryStream的毫秒=新的MemoryStream();    //去从文件的位置获取图像
    MS = GetImageAndPutIntoMemoryStream(RouteValueRefToImage);    //返回的输出作为一个文件
    返回新FileStreamResult(MS,图像/ JPEG);
 }

我知道这种方法可行,因为我用它来动态地生成基于captcha图像会话值的图像。这是pretty整洁 - 但我想用这个方法对所有图像检索

我想我在上面的例子或者想如果这是确定这样做是否需要更多的处理来执行,如果是这样,多少钱?例如,如果游客人数分别为1000例如倍增,将服务器中,然后在processingly图像的传送负担。

谢谢!


解决方案

一个类似的问题在之前(的一个ASP.Net MVC控制器可以返回一个图像?),看来,对性能的影响是非常小的,以提供图片直接出动作VS。作为接受的答案所指出的,差别似乎是一毫秒(在该试验的情况下,约13%)的数量级上。你可以在本地重新运行测试,看看不同的是你的硬件是什么。

最好的回答你的问题的如果您应该使用它是从这个<一个href=\"http://stackoverflow.com/questions/501893/mvc-rc-file-actionresult-performant/502176#502176\">answer (别人)类似的问题(重点煤矿):


  

不要担心以下几点:您将需要重新实现服务器的缓存策略,因为IIS管理,对静态文件直接要求。您还需要确保你管理你的客户端缓存正确的标题包含在响应中。 最后,就问自己,如果重新发明从服务器提供静态文件的方法是什么,提供应用程序的需要。


要解决你提供问题的具体情况:



  1.   

    应用安全检查


    您已经可以做到这一点使用的 IIS 7集成管道。从文档相关位:


      

    允许通过本机和托管模块所提供的服务适用于所有请求,无论处理程序。例如,管理表单验证可用于所有内容,包括ASP页面,CGI程序,和静态文件即可。




  2.   基于路由值提供文件服务的

    标准化法


    如果我在阅读文档正确,您可以在管道重新编写传入的URL直接指向静态资源,让IIS从那里处理请求插入模块够早。的(为了完整起见,也有关于测绘路线的法师此相关的问题:<一href=\"http://stackoverflow.com/questions/1146652/how-do-i-route-images-using-asp-net-mvc-routing\">How使用ASP.Net MVC路由做我的路线的图像?)


      

    赋权ASP.NET组件到由于其在服务器中管道放置提供功能,这是$ P $它们pviously不可用。例如,管理模块,提供要求重写功能,可在此之前的任何服务器处理,包括认证改写请求。


    也有一些pretty强大 URL重写功能来与IIS或多或少开箱



  3.   

    返回修改图像(如果需要)如不同dimentions(OK这只能尽量少用,所以不要这与上面的性能问题)。


    它看起来像一个模块,这确实是已可用于IIS。不知道这将属于服务从code图像与否,虽然,我猜它可能。



  4.   

    允许访问资源之前执行业务逻辑


    如果您要执行业务逻辑来产生所述资源(如)或者你所提到的captcha图像,然后是的,你根本别无选择,只能这样做。


I am creating a MVC 3 application (although just as applicable to other technologies e.g. ASP.NET Forms) and was just wondering if it is feasible (performance wise) to serve images from code rather than using the direct virtual path (like usual).

The idea is that I improve the common method of serving files to:

  1. Apply security checks
  2. Standardised method of serving files based on route values
  3. Returning modified images (if requested) e.g. different dimentions (ok this would only be used sparingly so don't relate this to the performance question above).
  4. Perform business logic before allowing access to the resource

I know HOW to do it but I don't know IF I should do it.

  1. What are the performance issues (if any)
  2. Does something weird happen e.g. images only load sequentially (maybe that's how HTML does it currently i am not sure - exposing my ignorance here).
  3. Anything else you can think of.

Hope this all makes sense!

Thanks, Dan.

UPDATE

OK - lets get specific:

What are the performance implications for using this type of method for serving all images in MVC 3 using a memory stream? Note: the image url would be GenericFetchImage/image1 (and just for simplicity - all my images are jpegs).

public FileStreamResult GenericFetchImage(string RouteValueRefToImage)
{
    // Create a new memory stream object
    MemoryStream ms = new MemoryStream();

    // Go get image from file location
    ms = GetImageAndPutIntoMemoryStream(RouteValueRefToImage);

    // return the output as a file
    return new FileStreamResult(ms, "image/jpeg");
 }

I know that this method works, because I am using it to dynamically generate an image based on a session value for a captcha image. It's pretty neat - but I would like to use this method for all image retrieval.

I guess I am wondering in the above example if this is ok to do or whether it requires more processing to perform and if so, how much? For example, if the number of visitors were to multiply by 1000 for example, would the server be then processingly burdened in the delivery of images..

THANKS!

解决方案

A similar question was asked before (Can an ASP.Net MVC controller return an Image?) and it appears that the performance implications are very small to serving images out of actions vs directly. As the accepted answer noted, the difference appears to be on the order of a millisecond (in that test case, about 13%). You could re-run the test locally and see what the difference is on your hardware.

The best answer to your question of if you should be using it is from this answer to (another) similar question (emphasis mine):

DO worry about the following: you will need to re-implement a caching strategy on the server, since IIS manages that for static files requested directly. You will also need to make sure you manage your client-side caching with the correct headers included in the response. Ultimately, just ask yourself if re-inventing a method of serving static files from a server is something that serves your application's needs.

To address the specific cases you provided with the question:

  1. Apply security checks

    You can already do this using the IIS 7 integrated pipeline. Relevant bit from documentation:

    Allowing services provided by both native and managed modules to apply to all requests, regardless of handler. For example, managed Forms Authentication can be used for all content, including ASP pages, CGIs, and static files.

  2. Standardised method of serving files based on route values

    If I'm reading the documentation correctly you can insert a module early enough in the pipeline to re-write incoming URLs to point directly to static resources and let IIS handle the request from there. (For the sake of completeness there also this related question regarding mapping routes to mages: How do I route images using ASP.Net MVC routing?)

    Empowering ASP.NET components to provide functionality that was previously unavailable to them due to their placement in the server pipeline. For example, a managed module providing request rewriting functionality can rewrite the request prior to any server processing, including authentication.

    There are also some pretty powerful URL rewrite features that come with IIS more or less out of the box.

  3. Returning modified images (if requested) e.g. different dimentions (ok this would only be used sparingly so don't relate this to the performance question above).

    It looks like a module that does this is already available for IIS. Not sure if that would fall under serving images from code or not though, I guess it might.

  4. Perform business logic before allowing access to the resource

    If you're performing business logic to generate said resources (like a chart) or as you mentioned a captcha image then yeah, you basically have no choice but to do it this way.

这篇关于ASP.NET(MVC)提供图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆