zoukankan      html  css  js  c++  java
  • ASP.NET URL重写浅析

    详细参见: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

    最简单的实现,就是在 Global.asax.cs 中 Application_BeginRequest  或者是 Application_AuthenticateRequest 事件处理中,对请求的URL进行判断并进行重写:

    protected void Application_BeginRequest(Object sender, EventArgs e)

            {

                HttpApplication app = (HttpApplication) sender;

                string requestedPath = app.Request.Path;

                string lookFor = @"^/webapptest/urlrewritetest/department/(\w+)\.aspx$";

                string sendTo = "/webapptest/urlrewritetest/webform2.aspx?dept=$1";

                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                if (re.IsMatch(requestedPath))

                {

                    string sendToUrl = re.Replace(requestedPath, sendTo);

                    app.Context.RewritePath(sendToUrl);

                }

            }

    重写主要利用了 HttpContext.RewritePath 方法。

    执行效果是将:

    http://localhost/WebAppTest/URLRewriteTest/department/Finance.aspx

    重写为:

    http://localhost/WebAppTest/URLRewriteTest/WebForm2.aspx?Dept=Marketing

  • 相关阅读:
    bzoj2045: 双亲数&bzoj1101: [POI2007]Zap
    spoj GCDEX
    jQuery Ajax
    jQuery 动画效果
    jQuery 事件处理API
    jQuery 常用getter&setter
    jQuery 文档操作
    jQuery 基础
    Vue2.2.0+新特性整理
    JavaScript中的HTTP
  • 原文地址:https://www.cnblogs.com/top5/p/1550898.html
Copyright © 2011-2022 走看看