使用微软URLRewriter.dll的url实现任意后缀名重写

<?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); 
	} 
} 

转载于:https://www.cnblogs.com/smartsmile/p/6234195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值