ASP.NET web application中的redirect

在开发ASP.NET MVC web application过程中,开发上线了新系统后,需要把老系统的url redirect新系统下

其中在项目系统目录下有一个文件 301RedirectsPages.config, 内容如下:

复制代码
<rewriteMaps>
    <rewriteMap name="Redirects">
           <add key="/contact-us.aspx" value="/contact-us" />
        <add key="/services-solutions" value="/services-and-solutions" />
        <add key="/sales-support" value="/sales-and-support" />
        <add key="/events-news" value="/events-and-news" />
        <add key="/services-solutions/services.aspx" value="/services-and-solutions/services" />
        <add key="/services-solutions/solutions.aspx" value="/services-and-solutions/solutions" />
        </rewriteMap>
</rewriteMaps>



在web.config文件中,在system.webServer下面,有一个rewrite节点,显示如下:
复制代码
<system.webServer>
    <rewrite  xdt:Transform="Replace">
      <rewriteMaps configSource="301RedirectsPages.config" />
      <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
        <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>
        <rule name="Redirects">
          <match url=".*" />
          <conditions>
            <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>
复制代码

这种情况下,在浏览器窗口输入URL:

localhost/contact-us.aspx 将自动redirect到 localhost/contact-us

localhost/services-solutions/services.aspx 自动redirect到 localhost/services-and-solutions/services

localhost/services-solutions/solutions.aspx 自动redirect到 localhost/services-and-solutions/solutions

但是,当你输入

localhost/services-solutions 不能自动redirect到 localhost/services-and-solutions

localhost/sales-support 不能自动redirect到 localhost/sales-and-support

localhost/events-news 不能自动redirect到 localhost/events-and-news

 

查找了半天的原因,才发现是在web.config 中的设置

appendQueryString="true" 应该改成 appendQueryString="false"

也就是这一行  <action type="Redirect" url="{C:1}" appendQueryString="true" />

应该改成 <action type="Redirect" url="{C:1}" appendQueryString="false" />

改成后的web.config文件如下:

复制代码
 <rewrite  xdt:Transform="Replace">
      <rewriteMaps configSource="301RedirectsPages.config" />
      <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
        <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>
        <rule name="Redirects">
          <match url=".*" />
          <conditions>
            <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>
复制代码

 

 



 

复制代码

posted on   新西兰程序员  阅读(247)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示