zoukankan      html  css  js  c++  java
  • ASP.NET 2.0 中的URL 重写技术

    说明:当我在开发一个网站的时候,我遇到了这样一个问题:

    这是一个有多个子域的网站,如:support.henryliu.com,product.henryliu.com,service.henryliu.com,客户想把这三个子站点指向同一个位置。他要实现无论何时,用户请求support.henryliu.com/default.aspx时,henryliu.com/support/default.aspx会被执行。 也就是说,他想把henryliu.com/support/default.aspx这个地址隐藏起来,不让用户看到。我们把改变原始请求URL的这个过程叫做URL重写。在这篇文章里,我将阐述关于URL重写的技术。

     

    实现URL重写的几种方法?

    l         利用Application_BeginRequestHttpContext类的Rewrite方法重写URL,这种方法比较简单易懂易用。

    l         开发ASP.NET Http Module来达到同样的目的

    l         开发ISAPI过滤器来截取请求完成重写

     

    在这里,我们将就第一种方法来阐述URL重写的实现

    Application_BeginRequest 事件

    它是HTTP管线处理被激发的第一个事件,是重写URL的最佳地方

     

    HttpContext

    这个类包含有关Http的特定信息,其中有Http Request,当然也有Response对像。

    这个类里面有一个Current静态属性,它里面包含当前应用程的信息。RewritePath方法是重写URL的关键。在2.0中有四个签名:

    public void RewritePath(string path);

    public void RewritePath(string path, bool rebaseClientPath);

    public void RewritePath(string filePath, string pathInfo, string queryString);

    public void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath);

     

    按步就搬

     

    1.         新建一个C# Web Application工程

    2.         打开WEB配置文件,加入下列代码

    <appSettings>
    <add key="productsSite" value="products"></add>
    <add key="servicesSite" value="services"></add>
    <add key="supportSite" value="support"></add>
    </appSettings>

     

    我们把相对应的文件夹名称放在这里,在后面的代码中将用到。

    3.         在工程里添加三个文件夹,Products,Support Services

    4.         在每个文件里面添加一个default.aspx页面

    5.         打开Global.asax看看事件句柄

     

    protected void Application_BeginRequest(object sender, EventArgs e)

    6.         把下面的代码加到上述事件里:


                string host, originalurl, newurl;
                host 
    = Request.Url.Host;
                originalurl 
    = Request.Url.PathAndQuery;

                
    switch (host)
                
    {
                    
    case "products.henryliu.com":
                        newurl 
    = "~/" +
                        ConfigurationSettings.AppSettings[
    "productsSite"]
                        
    + originalurl;
                        
    break;
                    
    case "services.henryliu.com":
                        newurl 
    = "~/" +
                        ConfigurationSettings.AppSettings[
    "servicesSite"]
                        
    + originalurl;
                        
    break;
                    
    case "support.henryliu.com":
                        newurl 
    = "~/" +
                        ConfigurationSettings.AppSettings[
    "supportSite"]
                        
    + originalurl;
                        
    break;
                    
    default:
                        newurl 
    = "~/" +
                        ConfigurationSettings.AppSettings[
    "supportSite"]
                        
    + originalurl;
                        
    break;
                }

                HttpContext.Current.RewritePath(newurl);


     

      让我们再来看看这段代码:

     

    首先我们用 Request.Url.Host 属性得到主机名,如:support.henryliu.com,其次还要获得当前路径的查询字符串。Switch语句我们用来根据当前用户的请求来判断真正要执行的页面请求。最后,我们调用RewritePath()方法重写当前请求的URL

     

    总节:在这篇文章里我们可以学习到怎样用Application_BeginRequest HttpContext.RewritePah()来重写URL。这是一个快速实现实际请求页面和我们看到的URL不同的方法。

    p.s.这是一篇译自Dotnetbips 网站的一篇技术文章,拿来共同分享.感谢作者Bipin Joshi   原文地址

  • 相关阅读:
    module.exports = $; $ is not defined
    npm run build 时 报 __webpack_public_path__ = window.webpackPublicPath; 中的windows未定义
    TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    windows package.json设置多个环境变量
    点击劫持ClickJacking
    判断数组的方法/判断JS数据类型的四种方法
    时间操作
    Ado调用存储过程
    layui表格增删改查与上传图片+Api
    layui表单与上传图片
  • 原文地址:https://www.cnblogs.com/lgp/p/530426.html
Copyright © 2011-2022 走看看