zoukankan      html  css  js  c++  java
  • 读书片断之 开发自定义HTTP模块

    阅读这本书第八章《ASP.NET请求处理框架》
    8.6 开发自定义HTTP模块
    以下的代码片断是为了实现一个支持URL重写的HTTP模块。
    多数的Web应用程序使用查询字符串在一个页面和另一个页面之间传输数据。这会给用户记忆和使用URL带来困难。
    如:http://localhost/Articles/Ariticles.aspx?AuthorName=Smith
    这样的URL不易记忆,且可能导致各种可用性问题。如果允许使用以下URL来访问一页面,那么站点访问者将感到更加轻松:
    http://localhost/Articles/Smith.aspx

    HTTP模块ArticlesModule
     
     1using System;
     2using System.Text.RegularExpressions;
     3using System.Web;
     4
     5namespace HttpModuleTest
     6{
     7    /// <summary>
     8    /// This Class finish URL rewriting.
     9    /// </summary>

    10    public class ArticlesModule : IHttpModule
    11    {
    12        public ArticlesModule()
    13        {
    14            //
    15            // TODO: Add constructor logic here
    16            //
    17        }

    18
    19        public void Init(HttpApplication context)
    20        {
    21            context.BeginRequest += new EventHandler(context_BeginRequest);
    22        }

    23
    24        public void Dispose()
    25        {
    26
    27        }

    28
    29        private void context_BeginRequest(object sender, EventArgs e)
    30        {
    31            HttpApplication app = sender as HttpApplication;
    32            HttpContext context = app.Context;
    33
    34            Regex regex = new Regex(@"Articles/(.*)\.aspx", RegexOptions.IgnoreCase);
    35            Match match = regex.Match(context.Request.Path);
    36
    37            if (match.Success)
    38            {
    39                string newPath = regex.Replace(context.Request.Path, @"Articles.aspx?AuthorName=$1");
    40                context.RewritePath(newPath);
    41            }

    42        }

    43    }

    44}

    45

  • 相关阅读:
    高斯消元学习
    HDU 4596 Yet another end of the world(解一阶不定方程)
    Codeforces Round #318 div2
    HDU 4463 Outlets(一条边固定的最小生成树)
    HDU 4458 Shoot the Airplane(计算几何 判断点是否在n边形内)
    HDU 4112 Break the Chocolate(简单的数学推导)
    HDU 4111 Alice and Bob (博弈)
    POJ 2481 Cows(线段树单点更新)
    HDU 4288 Coder(STL水过)
    zoj 2563 Long Dominoes
  • 原文地址:https://www.cnblogs.com/adam/p/758393.html
Copyright © 2011-2022 走看看