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映射到这些扩展名上,否则不能被解析。
  • 相关阅读:
    正则表达式
    文件上传例子
    如何做好数字化体验管理,了解一下?
    云原生背景下故障演练体系建设的思考与实践—云原生混沌工程系列之指南篇
    OpenKruise v1.0:云原生应用自动化达到新的高峰
    Spring Boot Serverless 实战系列“部署篇” | Mall 应用
    阿里云实时数仓Hologres年度发布,解读数仓新趋势
    基于 ASK + EB 构建容器事件驱动服务
    各位 PHPer,Serverless 正当时
    如何在零停机的情况下迁移 Kubernetes 集群
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760259.html
Copyright © 2011-2022 走看看