2013--01--01新年的第一天乖乖来教室学习 新的一年加油! 1 修改 HTTP 输出 (myModule) 2 URL重写 3 实现本网站图片保护功能

1 修改 HTTP 输出 (myModule)

**********************MyModule.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 模块与处理程序
{
    public class MyModule:IHttpModule//实现一个接口
    {
        void IHttpModule.Dispose()
        {
            //没有要释放的资源就什么都不用写了.
        }
        HttpContext _current = null;//上下文对象
        
        void IHttpModule.Init(HttpApplication context)
        {
            this._current = context.Context;//Context是当前应用程序对象的httpContext,表示的是当前用的
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        void context_EndRequest(object sender, EventArgs e)
        {
            _current.Response.Write("<!--Request end-->");
          
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            _current.Response.Write("<!--Request begin-->");

         
        }
    }
}

***********************default.aspx

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="模块与处理程序._default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    hello very one,it's nice to meet your here!
    </div>
    </form>
</body>
</html>
******************Web.config

<httpModules>
        <add name="myModule" type="模块与处理程序.MyModule"/>
            </httpModules>

2 URL重写

*******************UrlReWrite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 模块与处理程序
{
    public class UrlReWrite:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;//找到发生http请求的对象
            HttpRequest re = app.Request;//获取application对象的请求

            string path = re.Path;//获取请求的虚拟路径
            if (path.Substring(path.LastIndexOf("/") + 1) == "viewDetail.html")

            {
                string[] strs = path.Split('/');
                if (strs.Length != 4)
                {
                    return;
                }
                else
                {

                    string sid = strs[1];
                    string sname = strs[2];
                    app.Context.RewritePath("~/view.aspx?id=" + sid + "&name=" + sname);//重写一下路径
                }

            }

        }
    }
}

*********************default2.aspx

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="default2.aspx.cs" Inherits="模块与处理程序.default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="sid" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
                    ReadOnly="True" SortExpression="sid" />
                <asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
                <asp:HyperLinkField DataNavigateUrlFields="sid,sname"
                    DataNavigateUrlFormatString="{0}/{1}/viewDetail.html" HeaderText="操作"
                    Text="查看详情" />
                <asp:ImageField DataImageUrlFormatString="~/{0}" HeaderText="照片"
                    DataImageUrlField="photo">
                </asp:ImageField>
            </Columns>
        </asp:GridView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:studentConnectionString2 %>"
            SelectCommand="SELECT [sid], [sname], [sex], [age], [photo] FROM [student]">
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
*******************view.aspx

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="view.aspx.cs" Inherits="模块与处理程序.view" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
   
    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate><table></HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><%#Eval("sid")%></td>
    </tr>
    <tr>
    <td><%#Eval("sname")%></td>
    </tr>
    <tr>
    <tr>
    <td>
        <asp:Image ID="Image1" runat="server"  ImageUrl='<%#Eval("photo")%>'/></td>
    </tr>
    </ItemTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

***************view.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace 模块与处理程序
{
    public partial class view : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                binStudent();
            }

        }

        private void binStudent()
        {
            string sid = this.Request["id"];
            string constr = ConfigurationManager.ConnectionStrings["studentConnectionString2"].ConnectionString;
            string sql = "select sid,sname,age,sex from student where sid=" + sid;
            SqlConnection con = new SqlConnection(constr);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            this.Repeater1.DataSource = dt;
            this.Repeater1.DataBind();
        }
    }
}

*********************************Web.config

  <httpModules>
        <add name="urlReWrite" type="模块与处理程序.UrlReWrite"/>
      </httpModules>

3 实现本网站图片保护功能

**************************ImageDeal.cs

namespace 模块与处理程序
{
    public class ImageDeal:IHttpHandler
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            Image img = Image.FromFile(context.Request.PhysicalPath);
            Graphics g = Graphics.FromImage(img);
            g.DrawString("QQ",new Font("宋体",20,FontStyle.Bold),Brushes.Red,10,10);
            img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
           
            context.Response.Flush();
            context.Response.End();

        }
    }
}

**********************default3.aspx

 <div>
    <img src="http://localhost:52171/images/diaochan.jpg" />
    </div>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值