Rewrite URLs

原创 2004年11月03日 10:25:00

One of the more obscure - but potentially useful - methods in the portion of the .NET Framework that comprises ASP.NET is the HttpContext class's RewritePath method. Used internally by ASP.NET to strip session IDs from URLs when cookieless session state is enabled, RewritePath can also be used to "fake" URLs within an application. To demonstrate, consider the following ASPX file, which we'll call RewritePath.aspx:



    <h2><asp:Label ID="Output" RunAt="server" /></h2>




<script language="C#" runat="server">

void Page_Load (Object sender, EventArgs e)


    string id = Request.QueryString["ID"];

    if (id != null && id != String.Empty) {

        switch (id) {


        case "1":

            Output.Text = "Give me chastity and " +

                "continence, but not yet.";



        case "2":

            Output.Text = "A programmer is a device " +

                "for turning coffee into code.";



        case "3":

            Output.Text = "Blessed is the man who, " +

                "having nothing to say, abstains from " +

                 "giving wordy evidence of the fact.";






If this page is deployed in a virtual directory named Foo, then the following URLs invoke the page and display three different quotations:




So far, so good. Now suppose that you'd like users to be able to display these quotations by typing the following "phantom" URLs:




You can accomplish this little bit of magic - converting a URL of the form .../quotes/page1.aspx into a URL of the form .../rewritepath.aspx?id=1 on the fly - with HttpContext.RewritePath. The trick is to grab each request for /quotes/page1.aspx, /quotes/page2.aspx, and so on as it enters ASP.NET's HTTP pipeline and convert it into a request for /rewritepath.aspx?id=1, /rewritepath.aspx?id=2, and so on. Here's a Global.asax file that does just that:

<script language=C# runat="server">

void Application_BeginRequest (Object sender, EventArgs e)



    // TODO: Convert a path of the form

    // .../quotes/page1.aspx into a path of the form

    // .../rewritepath.aspx?id=1


    HttpContext context = HttpContext.Current;

    string oldpath = context.Request.Path.ToLower ();


    string token = "/quotes/page";

    int i = oldpath.IndexOf (token);

    int len = token.Length;


    if (i != -1) {

        int j = oldpath.IndexOf (".aspx");

         if (j != -1) {

            string id =

                oldpath.Substring (i + len, j - (i + len));

            string newpath =

                oldpath.Replace (token + id + ".aspx",

                "/rewritepath.aspx?id=" + id);

            context.RewritePath (newpath);





Application_BeginRequest fires at the beginning of every request. This implementation extracts the path portion of the URL targeted by the request (for example, /foo/quotes/page1.aspx), replaces it with a path that refers to the real page (for example, /foo/rewrite.aspx?id=1), and calls RewritePath to retarget the request. Try it: Drop RewritePath.aspx into wwwroot on your Web server and try to invoke it by typing this URL into your browser's address bar:


That should generate a "page not found" error. Now copy Global.asax to wwwroot and try again. This time, it should work just fine, thanks to the indirection afforded by Application_BeginRequest.

RewritePath has many applications. You can use it, for example, to convert query strings into path names so pages that use query string parameters to drive content can be bookmarked in browsers. You can also use it to obfuscate path names within your application for security reasons. Now that you know RewritePath exists, you may find other uses for it, too.

No urls, filenames, or requirements specified

看到这个问题你应该已经下载到setuptools-28.8.0了 如果没有 那么请把下面的python拷贝下来 然后执行 你就会得到了: #!/usr/bin/env python """ Set...
  • xbdf1234
  • xbdf1234
  • 2016年11月16日 10:33
  • 1488

Django入门:Django urls配置

from django.conf.urls import patterns, include, url from django.contrib import adminurlpatterns = pa...
  • foryouslgme
  • foryouslgme
  • 2016年05月10日 14:16
  • 778

dubbo Ignore empty notify urls for subscribe url

在本机启动dubbo服务的时候,后台有报错 服务端: ain  WARN multicast.MulticastRegistry:  [DUBBO] Ignore empty notify u...
  • jia18731601644
  • jia18731601644
  • 2017年05月11日 10:11
  • 1378

使用Hystrix守护应用(1) Hystrix(是Netflix(https...
  • xiao_jun_0820
  • xiao_jun_0820
  • 2016年02月17日 14:02
  • 1124

windows 下 easy_install 的安装

windows 下 easy_install 的安装
  • joeyon
  • joeyon
  • 2014年08月28日 09:53
  • 1068


django中urls的用法 url格式 URL解释: schema://host[:port#]/path/.../[?query-string][#anchor] schema:指...
  • sinat_25545645
  • sinat_25545645
  • 2017年08月12日 17:18
  • 346

URLs are treated as unique security origins.

出于安全考虑,最新版的浏览器默认本地的 XML 已经不能访问本地的XSLT 文件了,不过 HTML 文件仍能访问项目目录下的 CSS 文件.解决办法:只需要本地起个server即可: 1. Pyth...
  • soindy
  • soindy
  • 2017年05月04日 19:34
  • 862


本文介绍Django中的urls的配置:主项目与app之间的urls的配置,以及简单的用app/ 查询数据库,并显示在app/templates/xxxxxx.html中。 配...
  • u013045971
  • u013045971
  • 2015年09月06日 01:36
  • 3324

Clean URLs with Apache 2 on Ubuntu

Step 1 - Method A: "Virtual Host" Setup First, from the Linux command line, enable the rewrite modu...
  • jxncxgx
  • jxncxgx
  • 2014年06月14日 18:33
  • 435


  • youaremoon
  • youaremoon
  • 2016年02月29日 14:27
  • 3114
您举报文章:Rewrite URLs