【问题标题】:What is a Generic Handler in asp.net and its use?什么是 asp.net 中的通用处理程序及其用途?
【发布时间】:2010-02-25 08:24:28
【问题描述】:

我是 asp.net 的新手。我想了解 asp.net 中的通用处理程序 以及如何以及在何处使用?

你能帮帮我吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    通用处理程序是实现 System.Web.IHttpHandler 接口的 .NET 组件。任何实现 IHttpHandler 接口的类都可以作为传入 HTTP 请求的目标。页面也是通用处理程序。一般来说,通用处理程序具有 ASHX 的扩展。

    你可以找到例子here

    【讨论】:

      【解决方案2】:

      当您想避免常规 asp.net 页面的开销时,可以使用处理程序。实际示例包括图像处理或处理 AJAX 请求。

      Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components

      【讨论】:

        【解决方案3】:

        一些 ASP.NET 文件是动态生成的。它们是使用 C# 代码或磁盘资源生成的。这些文件不需要 Web 表单。相反,ASHX 通用处理程序是理想的。它可以从查询字符串中动态返回图像、写入 XML 或任何其他数据。

        【讨论】:

          【解决方案4】:

          Ashx 文件只不过是一个 aspx 页面。它们相当于用 C Sharp 或 Visual Basic.NET 编写的自定义处理程序,因为它们包含完全实现 IHttpHandler 的类。它们很方便,就像 ASPX 文件很方便一样。您只需浏览它们,它们就会自动编译。

          何时使用 WebForms(aspx)

          简单的 HTML 页面
          Asp.net 自定义控件
          简单的动态页面

          何时使用 Handlers(ashx)

          二进制文件
          动态图像视图
          性能关键网页
          xml 文件
          最小网页

          【讨论】:

            【解决方案5】:

            ASHX Generic Handler 是一个返回动态内容的概念。它用于返回 ajax 调用、来自查询字符串的图像、写入 XML 或任何其他数据。 我用它从查询字符串中返回 MP4 文件。请找到以下代码。

            using System;
            using System.Collections.Generic;
            using System.Configuration;
            using System.Data.SqlClient;
            using System.Linq;
            using System.Web;
            namespace ESPB.CRM.Web.UI.VideoUploading
            {
            
            public class FileCS : IHttpHandler
            {
            
                public void ProcessRequest(HttpContext context)
                {
                    int id = int.Parse(context.Request.QueryString["id"]);
                    byte[] bytes;
                    string contentType;
                    string strConnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
                    string name;
                    using (SqlConnection con = new SqlConnection(strConnString))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.CommandText = "select Name, Data, ContentType from VideoUpload where Id=@Id";
                            cmd.Parameters.AddWithValue("@Id", id);
                            cmd.Connection = con;
                            con.Open();
                            SqlDataReader sdr = cmd.ExecuteReader();
                            sdr.Read();
                            bytes = (byte[])sdr["Data"];
                            contentType = sdr["ContentType"].ToString();
                            name = sdr["Name"].ToString();
                            con.Close();
                        }
                    }
                    context.Response.Clear();
                    context.Response.Buffer = true;
                    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
                    context.Response.ContentType = contentType;
                    context.Response.BinaryWrite(bytes);
                    context.Response.End();
                }
            
                public bool IsReusable
                {
                    get
                    {
                        return false;
                    }
                }
             }
            }
            

            在这里我创建了 FileCS.ashx 文件。 我继承 IHttpHandler 接口的地方。并编写了 ProcessRequest(HttpContext context) 函数,该函数将在调用文件时默认运行。而 context.Request.QueryString[] 将获取参数。在这里,我将 id 作为参数传递。 IsReusable() 函数可用于获得良好的性能。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-11-27
              • 2015-06-20
              • 2013-08-12
              • 2016-10-10
              • 1970-01-01
              • 2021-07-01
              相关资源
              最近更新 更多