asp.net 偽靜態實現與次層網域泛解析

來源:互聯網
上載者:User

 asp教程.net 偽靜態實現與次層網域泛解析

app.context.rewritepath(path, string.empty, strb.tostring().split('?')[1]);
在web.config裡配置下:

<system.web>裡添加如下代碼。
<httpmodules>
<add type="common.urlrewriter" name="common" />

最後設定iis的時候記得要把iis的表頭設定為空白。
運行下你就能實現了
</httpmodules>

httpapplication app = (httpapplication)sender;
httpcontext context = app.context;
string url = context.request.url.absoluteuri; //完整url
string turl = url.split('.')[0];
string surl = turl.tolower().replace("http://", "");
stringbuilder strb = new stringbuilder();
strb.append(url);
strb.append(surl);

關於執行個體的詳細方法


 

protected void page_load(object sender, eventargs e)
    {
        checkdomain();
    }
    /// <summary>
    ///取得二級域主機頭值,並實行轉向
    /// </summary>
    public void checkdomain()
    {
        string hostname = httpcontext.current.request.url.host.tostring(); //擷取url主機地址
        string [] userhost = hostname.split(new char[] { '.' });  //數組,以“.”分隔

        //判斷次層網域地址是否符合abc.網域名稱.com 這種格式,及數組userhost長度不大於3,否則就跳傳到其它頁
        if (userhost.length > 3)
        {
            httpcontext.current.response.redirect("http://www.111cn.net/");        //跳轉到出錯頁面
            return;
        }

        string userdomainname = userhost[0].tostring();    //取得數組最前面一組值,及次層網域主機頭

        //進行特定判斷,及是不要用到做為次層網域的主機頭
        if (userdomainname.tolower() == "www" || userdomainname.tolower() == "網域名稱" || userdomainname == null || userdomainname.tostring() == "")
        {
            httpcontext.current.response.redirect("http://www.111cn.net/");        //跳轉到出錯頁面
            return;
        }
        else
        {
            //方法1
            string post = string.format("http://www.xxx.com/u/{0}/index.html", userdomainname);
            httpcontext.current.response.redirect(post);        //跳轉到使用者目錄,即是次層網域所要去到的目錄,當然你也可以跳傳至*.aspx?userid=xxx這樣的連結
            //方法2是通過session來把主機頭儲存進去,頁面載入的時候在這裡來進行判斷,然後頁面開啟的時候你輸入xxx.111cn.net就是xxx這個使用者的資訊
            //session["username"] = userdomainname;
            //方法3
            return;
        }
    }


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.