URL重写,友好的URL

博客展示了URL重写的相关配置与代码实现。通过XML配置文件定义匹配规则和替换规则,同时给出了C#代码实现URL重写的逻辑,包括处理请求、匹配规则、提取查询字符串和路径信息等步骤,实现友好的URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用URL重写,可以创造出友好的URL,可以一定程度隐藏查询参数,增加对搜索引擎的友好性,对旧系统的维护也很有益处。
1、添加IHttpModule的实现。
2、在Init(HttpApplication context)事件中注册BeginRequest事件。
3、在BeginRequest事件中根据一定规则使用RewritePath方法进行重写。
以下是代码部分。

web.config设置
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<configuration>
None.gif    
<configSections>
None.gif        
<section name="UrlMappings" type="Job.Personal.Components.UrlMappingsHandler, Job.Personal.Components" />
None.gif    
</configSections>
None.gif    
<UrlMappings>
None.gif        
<add match="Jobs\/([a-fA-F0-9]{32}|([a-fA-F0-9]{8})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{12}))\/viewjob.aspx" replace="Con001_ProjectManage/Job/viewjob.aspx?id=$1" />
None.gif        
<add match="Jobs\/([a-fA-F0-9]{32}|([a-fA-F0-9]{8})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{12}))\/viewcompany.aspx" replace="Con001_ProjectManage/Job/viewcompany.aspx?id=$1" />
None.gif    
</UrlMappings>
None.gif    
<system.web>
None.gif        
<httpModules>
None.gif            
<add type="Job.Personal.Components.UrlHandlerModule, Job.Personal.Components" name="UrlHandlerModule" />
None.gif        
</httpModules>
None.gif    
</system.web>
None.gif
</configuration>

IHttpModule的实现
None.gifpublic class UrlHandlerModule : IHttpModule
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public UrlHandlerModule()dot.gif{}
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
IHttpModule 成员#region IHttpModule 成员
InBlock.gif    
public void Init(HttpApplication context)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        context.BeginRequest 
+= new EventHandler(context_BeginRequest);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void Dispose()dot.gif{}
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
InBlock.gif    
private void context_BeginRequest(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        HttpApplication app 
= (HttpApplication)sender;
InBlock.gif
InBlock.gif        
string url = HttpContext.Current.Request.RawUrl;
InBlock.gif        RewriteUrl(url, app.Context);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void RewriteUrl(string urlToRewrite, HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        NameValueCollection mappings 
= (NameValueCollection)ConfigurationSettings.GetConfig("UrlMappings");
InBlock.gif        
for (int i = 0; i < mappings.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string matchExpression = Globals.GetApplicationPath() + mappings.GetKey(i);
InBlock.gif            Regex regEx 
= new Regex(matchExpression, RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.CultureInvariant|RegexOptions.Compiled);
InBlock.gif            
if (regEx.IsMatch(urlToRewrite))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (mappings[i] != String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//获取重写路径
InBlock.gif
                    string rewritePath = regEx.Replace(urlToRewrite, Globals.GetApplicationPath() + mappings[i]);
InBlock.gif
InBlock.gif                    
string querystring = String.Empty;
InBlock.gif                    
string pathInfo = String.Empty;
InBlock.gif                    
string path = String.Empty;
InBlock.gif
InBlock.gif                    
// 1. extract querystring
InBlock.gif
                    int qmark = rewritePath.IndexOf("?");
InBlock.gif                    
if (qmark != -1 || qmark + 1 < rewritePath.Length) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        querystring 
= rewritePath.Substring (qmark + 1);
InBlock.gif                        
if(qmark > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            rewritePath 
= rewritePath.Substring (0, qmark);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }
 
InBlock.gif
InBlock.gif                    
// 2. extract pathinfo
InBlock.gif
                    int pathInfoSlashPos = rewritePath.IndexOf("aspx/"+ 4;
InBlock.gif                    
if (pathInfoSlashPos > 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        pathInfo 
= rewritePath.Substring(pathInfoSlashPos);
InBlock.gif                        rewritePath 
= rewritePath.Substring(0, pathInfoSlashPos);
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
// 3. path
InBlock.gif
                    path = rewritePath;
InBlock.gif                    
InBlock.gif                    context.RewritePath(path, pathInfo, querystring);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class UrlMappingsHandler : NameValueSectionHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected override string KeyAttributeName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn "match"; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected override string ValueAttributeName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn "replace"; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

4、如果要为gif,jpg等非aspx,asmx之类的资源进行映射时,需要配置IIS映射到这些扩展名上,否则不能被解析。

转载于:https://www.cnblogs.com/BillChen/archive/2005/12/02/288952.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值