English 中文(简体)
用户控制箱中的通用手稿?
原标题:accessing generic handler in user control cs?

我带了一台通用手提器。

在我的用户组合中,我有一个用户控制,希望能够利用这一类静态方法。 但是,我不能使用 as类或方法。 是否重新获得任何违约或登记?

ghx代码:

    <%@ 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("");

求助台

问题回答

我不认为你能够从别处打电话,除非你给这几类人增加一个名字空间:

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.

无论如何,由于程序审查没有守则,手稿实际上取得了任何成就,因此,I m bit puzzled 所以,该守则完全载于.ashx

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

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

然后,请你打电话<代码>GetTileImage。 LoadImage , 载于您的法典的其他部分(在此测试的罚款)。 正如已经指出的那样,将LoadImage带入一个用户类别的更好做法是,你的手和用户会使用这个类别。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签