class1.cs

来自「對c#初學者參考..為課題asp.net 2.0教材代碼」· CS 代码 · 共 40 行

CS
40
字号
using System.Web;

public class SimpleRewriter : System.Web.IHttpModule
{

    HttpApplication _application = null;

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

    public void Dispose()
    {
    }

    private void context_BeginRequest(object sender, System.EventArgs e)
    {
        string requesturl =
            _application.Context.Request.Path.Substring(0,
                _application.Context.Request.Path.LastIndexOf("//")
            );

        //Here is where we parse the original request url to determine
        //the querystring parameters for the unfriendly url
        string[] parameters = requesturl.Split(new char[] { '/' });

        if (parameters.Length > 1)
        {
            string firstname = parameters[1];
            string lastname = parameters[2];

            //Rewrite the request path
            _application.Context.RewritePath("~/unfriendly.aspx?firstname=" +
                firstname + "&lastname=" + lastname);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?