Asp.net

從 URL 中刪除尾部斜杠

  • September 16, 2021

我想將“abc.aspx/”重定向到“abc.aspx”。如何才能做到這一點?

當請求的頁面以“/”結尾時,頁面被破壞。如何處理此類請求?是否有任何重寫規則可以添加到 web.config 文件中?

在您的 web.config 下system.webServer,添加

<rewrite>
 <rules>

   <!--To always remove trailing slash from the URL-->
   <rule name="Remove trailing slash" stopProcessing="true">
     <match url="(.*)/$" />
     <conditions>
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     </conditions>
     <action type="Redirect" redirectType="Permanent" url="{R:1}" />
   </rule>

 </rules>
</rewrite>

一些陷阱

在您的開發環境中,如果您在 Visual Studio Development Sever 下執行您的網站,您將無法看到此功能正常工作。您需要將應用程序配置為至少在 IIS Express 下執行。

當您部署您的網站並發現此功能在您的生產伺服器上不起作用時,那將是因為您錯過了配置的東西。常見錯誤之一是將overrideModeDefault屬性設置為applicationHost.config文件中Deny的規則。<sectionGroup name="rewrite">

如果您在共享主機環境中並且發現此功能不起作用,請詢問您的提供商是否已授予您配置此部分的權限。

來源:http ://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module

引用自:https://stackoverflow.com/questions/27287982