简体   繁体   中英

How to show images using image handler in asp.net

I recently decided to create my thumbnails using Image Handler.I mean using such as this address:

  <img src="Flower1.png?width=100&height=300"/>

I Googled it and reached no result. Most of tutorials said somehow that i should use like this address:

<img src="GetImage.ashx?file=~/Flower1.png&width=100&height=300"/>

But i dont want to use this address. And some tutorials which used my desired way, are too old to set IIS. Thanks for guidance.

Your question is pretty broad, but basically: under the covers you are going to need to have a asp.net handler, ie the GetImage.asxh file that will serve up the images - plenty of examples that you can google to find out how to do that, and once you have that working you can setup IIS redirect rules that will map the requests that come in as:

<img src="Flower1.png?width=100&height=300"/>

to

<img src="GetImage.ashx?file=~/Flower1.png&width=100&height=300"/>

Redirect rules starter:

http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

The user/website will only see the images with the standard urls that you want, but under-the-covers the redirection will take place and your handler can resize the images as needed on request.

You have to define, in the web.config, that you want for the path "Flower1.png" to use a specific Handler. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <httpHandlers>
        <!-- ImageHandler handlers -->
        <add verb="*" path="*Flower1.png" 
         type="skmHttpHandlers.ImageHandler, skmHttpHandlers" />
    </httpHandlers>
  </system.web>
</configuration>

This will set that for the "*Flower1.png" file, instead of sending the file itself, it'll run a http handler.

You can check this MSDN page for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM