實戰 HTTP 處理常式(HTTP Handler) (2) — 向HTTP 處理常式傳遞參數

來源:互聯網
上載者:User
我們在上一篇已經建立了一個最簡單的HTTP 處理常式“MyHandler.jxd”,現在我們來對她稍作修改,讓她可以接收一個參數並直接返回這個參數值。
Step1:修改 MyHandler.cs,讓她返回接收到的參數值。MyHandler.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace mylib.system.web
{
    public class MyHandler : System.Web.IHttpHandler
    {
        #region IHttpHandler 成員

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(System.Web.HttpContext context)
        {
            string n = context.Request.QueryString["n"];

            context.Response.ContentType = "text/html";
            context.Response.Write("<html><body>" + n + "</body></html>");
        }

        #endregion
    }
}

Step2:修改 Default.aspx ,讓 IFrame 向 MyHandler.jxd 傳遞一個參數。Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="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>
        <iframe src='~/MyHandler.jxd?n=impossible is nothing'></iframe>
    </div>
    </form>
</body>
</html>

Step3:將 Default.aspx 設為起始頁,按F5運行程式。

下載本篇全部原始碼

本篇到此結束,下一篇我們將作個稍微實用一點的東西。

本系列共6篇文章
    實戰 HTTP 處理常式(HTTP Handler) (6)——條碼隨意打
    實戰 HTTP 處理常式(HTTP Handler) (5)——不用臨時檔案,直接開啟動態產生的檔案
    實戰 HTTP 處理常式(HTTP Handler) (4)——與Web程式共用Session
    實戰 HTTP 處理常式(HTTP Handler) (3)——動態產生圖片
    實戰 HTTP 處理常式(HTTP Handler) (2)——向HTTP 處理常式傳遞參數   <- you are here. 
    實戰 HTTP 處理常式(HTTP Handler) (1)——建立一個最簡單的 HTTP Handler  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.