URL重写,友好的URL

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

web.config设置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="UrlMappings" type="Job.Personal.Components.UrlMappingsHandler, Job.Personal.Components" />
    
</configSections>
    
<UrlMappings>
        
<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" />
        
<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" />
    
</UrlMappings>
    
<system.web>
        
<httpModules>
            
<add type="Job.Personal.Components.UrlHandlerModule, Job.Personal.Components" name="UrlHandlerModule" />
        
</httpModules>
    
</system.web>
</configuration>

IHttpModule的实现
public class UrlHandlerModule : IHttpModule
{
    
public UrlHandlerModule(){}

    
IHttpModule 成员

    
private void context_BeginRequest(object sender, EventArgs e)
    
{
        HttpApplication app 
= (HttpApplication)sender;

        
string url = HttpContext.Current.Request.RawUrl;
        RewriteUrl(url, app.Context);
    }


    
private void RewriteUrl(string urlToRewrite, HttpContext context)
    
{
        NameValueCollection mappings 
= (NameValueCollection)ConfigurationSettings.GetConfig("UrlMappings");
        
for (int i = 0; i < mappings.Count; i++)
        
{
            
string matchExpression = Globals.GetApplicationPath() + mappings.GetKey(i);
            Regex regEx 
= new Regex(matchExpression, RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.CultureInvariant|RegexOptions.Compiled);
            
if (regEx.IsMatch(urlToRewrite))
            
{
                
if (mappings[i] != String.Empty)
                
{
                    
//获取重写路径
                    string rewritePath = regEx.Replace(urlToRewrite, Globals.GetApplicationPath() + mappings[i]);

                    
string querystring = String.Empty;
                    
string pathInfo = String.Empty;
                    
string path = String.Empty;

                    
// 1. extract querystring
                    int qmark = rewritePath.IndexOf("?");
                    
if (qmark != -1 || qmark + 1 < rewritePath.Length) 
                    
{
                        querystring 
= rewritePath.Substring (qmark + 1);
                        
if(qmark > 0)
                        
{
                            rewritePath 
= rewritePath.Substring (0, qmark);
                        }

                    }
 

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


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

                
break;
            }

        }

    }

}


public class UrlMappingsHandler : NameValueSectionHandler
{
    
protected override string KeyAttributeName
    
{
        
get return "match"; }
    }


    
protected override string ValueAttributeName
    
{
        
get return "replace"; }
    }

}

4、如果要为gif,jpg等非aspx,asmx之类的资源进行映射时,需要配置IIS映射到这些扩展名上,否则不能被解析。
原文地址:https://www.cnblogs.com/chenjunbiao/p/1760259.html