<?xml version="1.0"?> <!--先引用URLRewriter.dll,放置于Bin目录--> <configuration> <configSections> <!--配置重写规则节点--> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/> </configSections> <connectionStrings> <add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <!--重写规则--> <RewriterConfig> <Rules> <RewriterRule> <LookFor>~/product/([0-9]*).html</LookFor> <!--http://www.xxx.com/product/8001.html--> <SendTo>~/product.aspx?pid=$1</SendTo> <!--http://www.xxx.com/product.aspx?pid=8001--> </RewriterRule> <RewriterRule> <LookFor>~/product/([0-9]*)-([0-9]*).html</LookFor> <!--http://www.xxx.com/product/101-8001.html--> <SendTo>~/product.aspx?sid=$1&pid=$2</SendTo> <!--http://www.xxx.com/product.aspx?sid=101&pid=8001--> </RewriterRule> <RewriterRule> <LookFor>~/product/([0-9]*)/([0-9]*).html</LookFor> <!--http://www.xxx.com/product/101/8001.html--> <SendTo>~/product.aspx?sid=$1&pid=$2</SendTo> <!--http://www.xxx.com/product.aspx?sid=101&pid=8001--> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/> <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/> </httpHandlers> <compilation debug="true" targetFramework="4.0"/> </system.web> </configuration>
//利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 void Application_BeginRequest(object sender, EventArgs e) { string oldUrl = System.Web.HttpContext.Current.Request.RawUrl; //获取初始url //~/123.aspx → ~/Index.aspx?id=123 Regex reg = new Regex(@"^/d+.html"); if (reg.IsMatch(oldUrl)) { string id = reg.Match(oldUrl).ToString().Substring(1, reg.Match(oldUrl).ToString().LastIndexOf(".") - 1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/123 → ~/Index.aspx?id=123 Regex reg1 = new Regex(@"^/d+$"); if (reg1.IsMatch(oldUrl)) { string id = reg1.Match(oldUrl).ToString().Substring(1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/index/123 → ~/Index.aspx?id=123 Regex reg3 = new Regex(@"^/index/d+$"); if (reg3.IsMatch(oldUrl)) { string id = reg3.Match(oldUrl).ToString().Substring(7); Context.RewritePath("~/Index.aspx?id=" + id); } }