接口IHttpHandler的定义如下:
复制代码 代码如下:
interface IHttpHandler
{
void ProcessRequest(HttpContext ctx);
bool IsReuseable { get; }
1新建一网站,名为MyHttpHandlerTest
2右击添加,选择类库,取名为MyHttpHandler
3-在上一步新建的类库上右键添加System.Web引用
主要代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace MyHttpHandler
{
public class Class1:IHttpHandler,IRequiresSessionState
{
#region IHttpHandler成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("handler处理");
}
#endregion
}
}
4-在MyHttpHandler类库上右键,生成,取名为MyHttpHandler
5-在web.config中的system.web节点中天下如下节点
<httpHandlers>
<add verb="*" path="Handler1.aspx" type="MyHttpHandler.Class1,MyHttpHandler"/>
<!--
配置文件中的选项说明:
· verb可以是"GET"或"POST",表示对GET或POST的请求进行处理。"*"表示对所有请求进行处理。
· Path指明对相应的文件进行处理,"*.aspx"表示对发给所有ASPX页面的请求进行处理。可以指明路径,如"/test/*.aspx",表明只对test目录下的ASPX文件进行处理。
· Type属性中,逗号前的字符串指明HttpHandler的实现类的类名,后面的字符串指明Dll文件的名称。
格式如:type="自定义HttpHandler的实现类的全名,自定义HttpHandler的实现类的命名空间(即Dll名)"
或 type="自定义HttpHandler的实现类的全名"
-->
</httpHandlers>
6-在MyHttpHandlerTest右键添加引用,选择项目找到刚才编译后的.dll文件
7-运行Handler1.aspx,页面显示:
下面我们利用HttpHandler将一段文字生成于图片中
添加一个类,默认为Class.cs
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1:IHttpHandler
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool IsReusable
{
get { return true; }
}
private static Image OldImage = null;
private static Image GetOldImage(HttpContext context)
{
if (OldImage == null)
{
OldImage = Image.FromFile(context.Server.MapPath("~/Images/Old.jpg"));
}
return OldImage.Clone() as Image;
}
public void ProcessRequest(HttpContext context)
{
Image newimage = GetOldImage(context);
Graphics gh = Graphics.FromImage(newimage);
Font font = new Font("Monaco", 24.0f, FontStyle.Regular);
string writetext = HttpUtility.UrlEncode(context.Request.QueryString["writetext"]);
gh.DrawString(HttpUtility.UrlDecode(writetext), font, new SolidBrush(Color.LightBlue), 20.0f, newimage.Height - font.Height - 30);
newimage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
newimage.Dispose();
}
}
新建一个.aspx页面,添加一个HyperLink控件,再在其.cs文件中添加一段代码传值
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = "img.jpg?writetext=" + HttpUtility.UrlEncode("大蜗牛");
}
另外还需在web.config文件中将httpHandlers节点中改为如下
<add verb="*" path="*.jpg" type="Class1"/>
表明对所有的.jpg格式的文件才会处理
参考《道不远人 深入解析asp.net 2.0控件开发》
复制代码 代码如下:
interface IHttpHandler
{
void ProcessRequest(HttpContext ctx);
bool IsReuseable { get; }
1新建一网站,名为MyHttpHandlerTest
2右击添加,选择类库,取名为MyHttpHandler
3-在上一步新建的类库上右键添加System.Web引用
主要代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace MyHttpHandler
{
public class Class1:IHttpHandler,IRequiresSessionState
{
#region IHttpHandler成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("handler处理");
}
#endregion
}
}
4-在MyHttpHandler类库上右键,生成,取名为MyHttpHandler
5-在web.config中的system.web节点中天下如下节点
<httpHandlers>
<add verb="*" path="Handler1.aspx" type="MyHttpHandler.Class1,MyHttpHandler"/>
<!--
配置文件中的选项说明:
· verb可以是"GET"或"POST",表示对GET或POST的请求进行处理。"*"表示对所有请求进行处理。
· Path指明对相应的文件进行处理,"*.aspx"表示对发给所有ASPX页面的请求进行处理。可以指明路径,如"/test/*.aspx",表明只对test目录下的ASPX文件进行处理。
· Type属性中,逗号前的字符串指明HttpHandler的实现类的类名,后面的字符串指明Dll文件的名称。
格式如:type="自定义HttpHandler的实现类的全名,自定义HttpHandler的实现类的命名空间(即Dll名)"
或 type="自定义HttpHandler的实现类的全名"
-->
</httpHandlers>
6-在MyHttpHandlerTest右键添加引用,选择项目找到刚才编译后的.dll文件
7-运行Handler1.aspx,页面显示:
下面我们利用HttpHandler将一段文字生成于图片中
添加一个类,默认为Class.cs
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1:IHttpHandler
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool IsReusable
{
get { return true; }
}
private static Image OldImage = null;
private static Image GetOldImage(HttpContext context)
{
if (OldImage == null)
{
OldImage = Image.FromFile(context.Server.MapPath("~/Images/Old.jpg"));
}
return OldImage.Clone() as Image;
}
public void ProcessRequest(HttpContext context)
{
Image newimage = GetOldImage(context);
Graphics gh = Graphics.FromImage(newimage);
Font font = new Font("Monaco", 24.0f, FontStyle.Regular);
string writetext = HttpUtility.UrlEncode(context.Request.QueryString["writetext"]);
gh.DrawString(HttpUtility.UrlDecode(writetext), font, new SolidBrush(Color.LightBlue), 20.0f, newimage.Height - font.Height - 30);
newimage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
newimage.Dispose();
}
}
新建一个.aspx页面,添加一个HyperLink控件,再在其.cs文件中添加一段代码传值
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = "img.jpg?writetext=" + HttpUtility.UrlEncode("大蜗牛");
}
另外还需在web.config文件中将httpHandlers节点中改为如下
<add verb="*" path="*.jpg" type="Class1"/>
表明对所有的.jpg格式的文件才会处理
参考《道不远人 深入解析asp.net 2.0控件开发》
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】