Search code examples
c#.netashx

How do you programatically call an ashx file from dot.net?


I have been provided with an ashx "service" that returns an image. I'm new to ashx files - so don't know how to handle it.

I need to stream the image into a byte[] so I can copy it somewhere else. How do I do that?


Solution

  • You can use WebClient.DownloadData pointing to asxh.

    So let me give you a sample. Let's say you have image located on asp.net page, like this:

    <img src="http://someServer/someSite/MyHandler.ashx?id=myId"/>
    

    In this case you can use following code:

            using (System.Net.WebClient wclient = new System.Net.WebClient())
            {
                byte[] data = wclient.DownloadData(
                    "http://someServer/someSite/MyHandler.ashx?id=myId");  
            }
    

    Alternatively you can use WebRequest