在ASP.NET中重写URL

 

经常在网上看到这样的地址http://XXX.XXX.com/20070726/251251116.htm,其实这些静态文件有可能根本不存在,而客户在浏览时,是不知道站点上根本不存在这个文件。这是怎么实现的呢?对,是URL重写,这样有什么好处呢。我个人认为有以下几个优点:

1.            易于理解。比如新闻栏目http://someblog.com/news/2007/01/28/XXX.htm,显示的是2007128日的某条新闻。

2.            易于键入。

3.            可以看出站点的结构。

4.            可以有效的防SQL注入。http://someblog.com/news/display.aspx?year=2007&month=7&day=21&id=XXX,客户端可以清楚地看到querystring的值,若过滤不严,就出现明显的SQL注入。而把上面的地址在客户端显示为http://someblog.com/news/2007/01/28/XXX.htm,若在客户端修改这几个参数值,若没有相应的内容,就出现找不到这个页面,而不会暴露出数据库等相关信息。做了地址重写,再加上对他们过滤,安全系数比没有重写URL时要大大的提高。即使你不会过滤,我用注入工具scan了下代码,没有找到注入地址。

那重写URL在站点如何实现呢。有以下几点:

url rewriting的方法

原文地址http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

 

 

1. 使用Request.PathInfo代替QueryStrings(不用了)

url: http://www.store.com/products.aspx?category=books

目标url: http://www.store.com/products.aspx/Books

方法:    

Function GetCategory() As String

        If (Request.PathInfo.Length = 0) Then

            Return ""

        Else

            Return Request.PathInfo.Substring(1)

        End If

End Function

 

2. 使用HttpModule

url: http://www.store.com/products.aspx?category=books

目标url: http://www.store.com/products/Books.aspx

方法:

    void Application_BeginRequest(object sender, EventArgs e) {

        string fullOrigionalpath = Request.Url.ToString();

        if (fullOrigionalpath.Contains("/Products/Books.aspx")) {

            Context.RewritePath("/Products.aspx?Category=Books");

        }

        else if (fullOrigionalpath.Contains("/Products/DVDs.aspx")) {

            Context.RewritePath("/Products.aspx?Category=DVDs");

        }

   

3. 使用UrlRewriter.net(http://urlrewriter.net/)UrlRewriting.net(http://www.urlrewriting.net/en/Default.aspx)

配置在web.config

<?xml version="1.0"?>

 

<configuration>

 

 <configSections>

    <section name="rewriter" 

             requirePermission="false"

             type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

 </configSections>

 

 <system.web>

     

    <httpModules>

      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>

    </httpModules>

   

 </system.web>

 

 <rewriter>

    <rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />

    <rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />

    <rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />

    <rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" /><!--使用正则表达式-->

 </rewriter> 

 

</configuration> 

4. 无后缀页面

 IIS7: web.config中配置

 <?xml version="1.0" encoding="UTF-8"?>

 

<configuration>

 

 <configSections>

    <section name="rewriter"

             requirePermission="false"

             type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

  </configSections>

 

 <system.web>

     

    <httpModules>

      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />

    </httpModules>

   

 </system.web>

 

 <system.webServer>

 

    <modules runAllManagedModulesForAllRequests="true">

      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />

    </modules>

 

    <validation validateIntegratedModeConfiguration="false" />

 

 </system.webServer>

 

 <rewriter>

    <rewrite url="~/products/(.+)" to="~/products.aspx?category=$1" />

 </rewriter>

 

</configuration>

 

 IIS5 and IIS6中使用ISAPI Filter Helicon Tech's ISAPI Rewrite(http://www.isapirewrite.com/), Ionic's ISAPI Rewrite(http://cheeso.members.winisp.net/IIRF.aspx)

5. 处理form postback链接

 增加.browser文件

 详细见http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

 

具体的实例代码,我会在下篇中给出,请大家关注。谢谢!

 

 

引自: www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

http://blog.chinaunix.net/u/4606/showart_250841.html

原文地址:https://www.cnblogs.com/kevinlzf/p/832530.html