ASP.NET(MVC)服务图像

我正在创建一个MVC 3应用程序(尽管适用于其他技术,例如ASP.NET Forms),并且只是想知道是否可行(性能明智)从代码提供图像而不是使用直接虚拟路径(如通常那样).

我的想法是,我改进了提供文件的常用方法

>应用安全检查
>基于路线值提供文件的标准化方法
>返回修改后的图像(如果请求),例如不同的维度(确定这只会谨慎使用,所以不要将其与上述性能问题联系起来).
>在允许访问资源之前执行业务逻辑

我知道怎么做,但我不知道我应该这样做.

>什么是性能问题(如果有的话)
>有什么奇怪的事情发生,例如图像只是按顺序加载(也许这就是HTML目前我不确定的方式 – 在这里暴露我的无知).
>你能想到的任何其他事情.

希望这一切都有意义!

谢谢,
担.

UPDATE

好的 – 让我们具体:

使用此类方法使用内存流为MVC 3中的所有图像提供服务会对性能产生什么影响?注意:图像URL将是GenericFetchImage / image1(为简单起见 – 我的所有图像都是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");
 }

我知道这种方法有效,因为我使用它来根据验证码图像的会话值动态生成图像.它非常整洁 – 但我想使用这种方法进行所有图像检索.

我想我在上面的例子中想知道这是否可行,或者是否需要更多的处理才能执行,如果是,那么多少?例如,如果访问者的数量乘以1000,那么服务器是否会在交付图像时处理负担.

谢谢!

解决方法

之前( Can an ASP.Net MVC controller return an Image?)也提出了一个类似的问题,并且看起来直接用行动提供图像的性能影响非常小.正如所接受的答案所指出的那样,差异似乎是毫秒级(在该测试案例中,约为13%).您可以在本地重新运行测试,看看您的硬件有何不同.

关于你是否应该使用它的问题的最佳答案是从这个answer到(另一个)类似的问题(强调我的):

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.

解决您提供的具体问题:

>

Apply security checks

您可以使用IIS 7 integrated pipeline执行此操作.文档中的相关位:

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.

>

Standardised method of serving files based on route values

如果我正确地阅读文档,您可以在管道中尽早插入模块以重新编写传入的URL以直接指向静态资源,并让IIS从那里处理请求. (为了完整起见,还有关于将法律路线映射到法师的相关问题: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.

还有一些非常强大的URL rewrite features,它带有或多或少的开箱即用的IIS.
>

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).

看起来0700已经可用于IIS了.不确定这是否会落在代码服务图像之下,但我猜它可能.
>

Perform business logic before allowing access to the resource

如果你正在执行业务逻辑来生成所述资源(如chart)或者你提到的验证码图像那么是的,你基本上别无选择,只能这样做.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....