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映射到这些扩展名上,否则不能被解析。
  • 相关阅读:
    第七节:Asp.Net Core内置日志、将EF生成的SQL输出到控制台
    自学Zabbix3.6.2-触发器triggers severity严重程度
    自学Zabbix3.6.1-触发器triggers创建
    自学Zabbix3.5.7-监控项item-Applications
    自学Zabbix3.5.6-监控项item-Value mapping值映射
    自学Zabbix3.5.5-监控项item-User parameters(自定义key)
    自学Zabbix3.5.4-监控项item-History and trends
    自学Zabbix3.5.3-监控项item-zabbix agent 类型所有key
    自学Zabbix3.5.2-监控项item-types监控类型
    自学Zabbix3.5.1-监控项item-key介绍
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760259.html
Copyright © 2011-2022 走看看