asp.net對於URL重寫,支援無尾碼url請求

來源:互聯網
上載者:User

通過簡單對iis配置,再利用urlwriter就可以完美解決url重寫的問題

可以將http://abc.domain.com/blog

轉向到http://www.domain.com/xxx.aspx?username=abc

當然首先要將主機的泛網域名稱支援開啟。

做法是

A。開啟IIS,右擊網站(虛擬目錄)-》屬性-》主目錄-》配置-》插入-》C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,不確認檔案存在-》確定

B。修改webconfig:

<RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>http://localhost/(blog)</LookFor>
        <SendTo>~/sdfsdf.asxp?tag=$1</SendTo>
      </RewriterRule>
   </Rules>
</RewriterConfig>

C。修改UrlRewriter
從微軟下載源碼,

1.BaseModuleRewriter.cs

protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        {
             HttpApplication app = (HttpApplication) sender;
             Rewrite(app.Request.Path, app);
         }
改為

protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        {
             HttpApplication app = (HttpApplication) sender;
             Rewrite(app.Request.Url.AbsoluteUri, app);
         }

就是將 app.Request.Path 替換成了 app.Request.Url.AbsoluteUri

2.ModuleRewriter.cs

for(int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
 
                // Create a regex (note that IgnoreCase is set)
                 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
 
                // See if a match is found
                if (re.IsMatch(requestedPath))
                {
                    // match found - do any replacement needed
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
 
                    // log rewriting information to the Trace object
                     app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
 
                    // Rewrite the URL
                     RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;        // exit the for loop
                 }
             }
改為

for(int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + rules[i].LookFor + "$";
 
                // Create a regex (note that IgnoreCase is set)
                 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
 
                // See if a match is found
                if (re.IsMatch(requestedPath))
                {
                    // match found - do any replacement needed
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
 
                    // log rewriting information to the Trace object
                     app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
 
                    // Rewrite the URL
                     RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;        // exit the for loop
                 }
             }

string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

改成了

string lookFor = "^" + rules[i].LookFor + "$";

OK。

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/zj1103/archive/2009/04/18/4089664.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.