Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在用户控件cs中访问通用处理程序?_C#_Asp.net_.net - Fatal编程技术网

C# 在用户控件cs中访问通用处理程序?

C# 在用户控件cs中访问通用处理程序?,c#,asp.net,.net,C#,Asp.net,.net,我已经制作了一个通用的handler.ashx,它位于根目录下 在我的UserControls文件夹中,我有一个用户控件,它想要访问这个ashx类的静态方法。但我无法访问ashx类或其方法。它需要任何参考或注册吗 ashx代码: <%@ WebHandler Language="C#" Class="GetTileImage" %> using System; using System.Collections.Generic; using System.Linq; usin

我已经制作了一个通用的handler.ashx,它位于根目录下

在我的UserControls文件夹中,我有一个用户控件,它想要访问这个ashx类的静态方法。但我无法访问ashx类或其方法。它需要任何参考或注册吗

ashx代码:

    <%@ 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;
    }
}

Help plz

我认为您无法从其他地方调用代码,除非您为类添加名称空间:

namespace MyNamespace 
{

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

}
MyNamespace
应替换为您在其余代码中使用的任何名称空间


在任何情况下,我都有点困惑,为什么这段代码会出现在
.ashx
中——因为ProcessRequest没有代码,所以处理程序实际上不会做任何事情

不,您无法访问代码隐藏(aspx、ascx等)中的
泛型
处理程序类方法。您必须在
App\u code
文件夹下创建一个静态(非必需)类(文件),并在其中移动此方法

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

我认为这可能只是因为您的代码在ASHX文件中,如果您使用代码隐藏文件,应该可以。e、 g:

GetTileImage.ashx:

GetTileImage.ashx.cs:

// < using statements here...>

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

         public static Bitmap LoadImage()
         {
             // < code here... >
         }
    }
}
/<在此处使用语句…>
名称空间MyNamespace
{
公共类GetTileImage:IHttpHandler
{
//<包括必要的覆盖…>
公共静态位图LoadImage()
{
//
}
}
}

然后,您会发现您可以在代码的其他地方调用
GetTileImage.LoadImage
(此处测试良好)。正如前面所指出的,最好将LoadImage移动到处理程序和用户控件都将使用的实用程序类中。

这不应该是问题,也不需要任何特殊要求,您可以包含一些示例源代码吗?为什么处理程序上有此方法?你想通过打电话来实现什么?我不认为从控件调用处理程序中的代码可能是一个好主意。谢谢Jonas H,我正在改进一些工作。@PirateKitten,请查看我的编辑,我添加了代码。我下面的回答提供了一些见解吗?你不能是什么意思?请说明。你的意思是不能在cs后面的页面或cs后面的用户控件中访问ashx吗?我尝试了你的方法,但仍然无法访问ashx类或其静态方法。是否需要任何名称空间添加或引用等?一般来说,我们可以在PageBehindCS类中调用ashx页面类或静态方法吗?
// < using statements here...>

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

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