2016-02-12 106 views
-1

我想重寫一個url以匹配頁面文件名的確切名稱。 我的意思是:我有一個http://example.com/PagePage.aspx,並在瀏覽器欄上打字http://example.com/pagepage.aspx我想再次獲得http://example.com/PagePage.aspx在web.config中重寫URL c#

當然,上鍵入字符的任意組合,即Pagepage.aspx,pagePage.aspx相同,等等...

我試過這樣:

<system.webServer> 
    <rewrite> 
     <rules> 
     <rule name="SpecificRewrite" stopProcessing="true"> 
      <match url="^pagepage$" /> 
      <match url="^Pagepage$" /> 
      <match url="^pagePage$" /> 
      <action type="Rewrite" url="PagePage.aspx" /> 
     </rule> 
     </rules> 
    </rewrite> 
</system.webServer> 

,但我得到「500 - 內部服務器錯誤」。

所以我也試過這樣:

<system.webServer> 
    <rewrite> 
     <rules> 
     <rule name="SpecificRewrite" stopProcessing="true"> 
      <match url="^pagepage\/?$" /> 
      <match url="^Pagepage\/?$" /> 
      <match url="^pagePage\/?$" /> 
      <action type="Redirect" url="/PagePage.aspx" /> 
     </rule> 
     </rules> 
    </rewrite> 
</system.webServer> 

,但我得到一個無限循環

我嘗試使用的Global.asax

 protected void Application_BeginRequest(object sender, EventArgs e) 
     { 
      HttpContext context = HttpContext.Current; 
      string path = context.Request.Path; 
      if (path.Equals("/pagepage.aspx")) 
      { 
       context.RewritePath(path.Replace("pagepage.aspx", "PagePage.aspx")); 
// or context.RewritePath("PagePage.aspx"); is the same 
      } 
     } 

但要獲得同樣的,一次。 ..我得到一個無限循環錯誤...

我GOOGLE了這個問題,但...我發現的方法,更多或更少,我已經嘗試過。我錯在哪裏?

對此有何建議? 謝謝您提前

回答

0

首先,一個規則不能有多個匹配元素。這可能是你得到500錯誤的原因。其次,我認爲你的意思是重定向而不是重寫。因此,以下內容將檢查結束pagepage.aspx的請求,而不管字母大小寫和條件是否將檢查URL是否以PagePage.aspx結尾,因此它不會無限循環。這裏是:

<rule name="SpecificRewrite" stopProcessing="true"> 
<match url="^pagepage.aspx$" /> 
<conditions> 
    <add input="{REQUEST_URI}" pattern="PagePage.aspx" ignoreCase="false" negate="true" /> 
</conditions> 
<action type="Redirect" url="PagePage.aspx" /> 
</rule>