URL参数大家都应该明白怎么回事吧,比如http://spaces.msn.com/blog.aspx?name=dcsoft, name就是参数,参数值是dcsoft,然后交给blog.aspx处理,blog.aspx能读取这些参数。
虚拟路径是什么呢,博客大家都用过吧,比如http://spaces.msn.com/dcsoft/ ,dcsoft/就是虚拟路径。IIS里面有一项技术是虚拟目录,可以虚拟出来各种目录,但是像博客这种东西,是不能用虚拟目录的,因为,这么多的博客,不可能每人都在IIS建一个虚拟目录,这个不现实!
博客应该是有一个模板,http://spaces.msn.com/后面的路径作为参数,传递到某个地方进行处理,然后套用模板显示出来。就是说技术上使用URL参数传递,显示上使用虚拟目录,使用户感到整个URL很友好。
了解了这项技术是什么以后,下面我们来研究该怎么做。
原理上讲,如果IIS不拦截这个虚拟目录,就会把请求传到ASP.NET应用程序里面来,如果ASP.NET也不能处理,那么就会返回报告“该页无法显示”,所以关键是让IIS放行这个虚拟目录,ASP.NET处理这个虚拟目录。IIS放行很简单,只要不在IIS设置这个同名的虚拟目录或者真实目录就可以了;如何让你的ASP.NET程序处理虚拟目录的请求呢,这是重点和难点。
1、创建一个类,用来处理ASP.NET全局的请求,这个类要继承IhttpHandler
public class deal:IHttpHandler
2、这个类要实现IHttpHandler接口,成员有2个
bool IHttpHandler.IsReusable属性,此属性返回true就行了,为什么我也不知道 - -6
bool IHttpHandler.IsReusable
{
get
{
return true;
}
}
void IHttpHandler.ProcessRequest(HttpContext context)方法,此方法是处理URL重定向的核心,你想怎样重定向的业务逻辑,都写在这里。
void IHttpHandler.ProcessRequest(HttpContext context)
{
//获取当前访问的重写过的虚假URL,path值例:/websitetest/user/abc
string path = context.Request.Path.ToLower();
//分析出参数,path值例:abc
path = path.Substring(path.LastIndexOf("/")+1);
//Default.aspx?name=abc
context.Server.Execute("~/Default.aspx?name="+path);
}
3、最后要在Web.Config文件里面定义这个由ASP.NET处理的虚拟路径,以下代码加在<system.web>节点里面
<httpHandlers>
<!-- verb动词,请求类型get or post or * -->
<!-- path被请求文件的位置和文件名 -->
<!-- type处理类 -->
<add verb="*" path="user/*" type="deal" />
</httpHandlers>
注意:我说明一下,我当初犯的一个错误,很郁闷。path不能直接写成path="*",一定要虚拟的加一层user/或者其他名字,因为处理参数的Default.aspx不能和要虚拟的路径在同一层目录下,否则会循环调用。
4、如果你用VS2005的文件系统+VS虚拟Web服务器调试的话,能成功,但是驾到IIS上面,就会出错,为什么呢?因为IIS还需要配置一下,才能使用。
在IIS里面,找到你想要使用URL重定向技术的那个节点(应该是个ASP.NET应用程序或虚拟目录),右键,属性,目录选项卡,单击“配置”按钮,在应用程序扩展里面找到扩展名是aspx的可执行文件dll的路径,复制下来,在通配符应用程序映射里面点击插入,然后粘贴,然后确定到底,就好了。
http://blog.csdn.net/dcsoft/archive/2006/08/21/1103841.aspx
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
浙公网安备 33010602011771号