user576510
user576510

Reputation: 5915

accessing generic handler in user control cs?

I have made a generic handler .ashx which is placed on root.

In my UserControls folder I have a user control which want to access this ashx class's static method. But I cannot access ashx class or its method. Does it requried any refrence or registration ?

ashx code:

    <%@ WebHandler Language="C#" Class="GetTileImage" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
public class GetTileImage : IHttpHandler, IRequiresSessionState
{
    const string c_key = "dzi";
    public void ProcessRequest(HttpContext context)
    {
        //context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(60));  
    }
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

    public static Bitmap LoadImage(string imageUrl)
    {
        Dictionary<string, Bitmap> images = (Dictionary<string, Bitmap>)HttpContext.Current.Session[c_key];
        if (images == null)
        {
            images = new Dictionary<string, Bitmap>();
            HttpContext.Current.Session[c_key] = images;
        }
        Bitmap bmp = null;
        if (!images.ContainsKey(imageUrl))
        {
            try
            {
                string url = imageUrl;
                if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
                    url = HttpContext.Current.Server.MapPath(url); WebClient wc = new WebClient(); Stream fs = wc.OpenRead(url); bmp = new Bitmap(fs); fs.Close();
            }
            catch { return null; }
        } images.Add(imageUrl, bmp); if (images.Count > 5)
        {
            Dictionary<string, Bitmap>.KeyCollection.Enumerator e = images.Keys.GetEnumerator();
            e.MoveNext();
            string key = e.Current;
            images.Remove(key);
        }
        return bmp;
    }
}

User Control where I am accessing this:

 Bitmap bmp = GetTileImage.LoadImage("");

Help plz

Upvotes: 0

Views: 1159

Answers (3)

Kasaku
Kasaku

Reputation: 2192

I think this could just be because your code is in your ASHX file, if you used a code-behind file it should be fine. e.g.:

GetTileImage.ashx:

<%@ WebHandler Language="C#" CodeBehind="GetTileImage.ashx.cs" Class="MyNamespace.GetTileImage" %>

GetTileImage.ashx.cs:

// < using statements here...>

namespace MyNamespace
{
    public class GetTileImage: IHttpHandler
    {
         // < include necessary overrides... >

         public static Bitmap LoadImage()
         {
             // < code here... >
         }
    }
}

You should then find you can call GetTileImage.LoadImage elsewhere in your code (tested fine here). As pointed out though already, it would be better to move the LoadImage into a utility class that both your handler and your UserControls will use.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

No you can't access the Generic handler class method in code behind (aspx,ascx etc). You should have to create a static (not necessary) class (file) under App_Code folder and move this method in it.

public class GetTileImage
{
 public static Bitmap LoadImage(string imageUrl)
    {
     ..
    }
}

Upvotes: 1

Jeremy McGee
Jeremy McGee

Reputation: 25210

I don't think you'll be able to call the code from elsewhere unless you add a namespace for the class:

namespace MyNamespace 
{

    public class GetTileImage : IHttpHandler, IRequiresSessionState
    {
    // etc. etc.
    }

}

MyNamespace should be replaced with whatever namespace you're using for the rest of your code.

In any case I'm a bit puzzled why this code is in an .ashx at all -- as it stands, because ProcessRequest has no code, the handler won't actually do anything.

Upvotes: 1

Related Questions