zoukankan      html  css  js  c++  java
  • 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映射到这些扩展名上,否则不能被解析。
  • 相关阅读:
    沉默
    抱冰握火
    数据库原理-SQL查询语句
    简单算法的实现——集合
    团队总结
    个人作业----项目测试
    团队项目-Beta冲刺
    团队项目-Alpha版本发布1
    团队项目-----系统设计 认真不马虎队
    团队项目----需求分析 认真不马虎队
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760259.html
Copyright © 2011-2022 走看看