【问题标题】:Removing .aspx extension from URL using URL Rewriting使用 URL 重写从 URL 中删除 .aspx 扩展名
【发布时间】:2013-09-15 10:37:38
【问题描述】:

我正在我的项目中实现 URL 重写。 我添加了使用 URL 重写从 IIS 重写的规则。 下面是我添加规则的 web.config 文件的代码:

<system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="URLRedirect" stopProcessing="true">
                    <match url="^([a-z0-9/]+).aspx$" />
                    <action type="Redirect" url="{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

但问题是我已经编写了删除扩展名的规则,即 .aspx,我希望我的 URL 看起来像

http://localhost:58370/URLRedirect/Default.

但现在它显示为http://localhost:58370/URLRedirect/ 这个问题怎么解决.....

【问题讨论】:

    标签: c# url-rewriting


    【解决方案1】:

    你可以这样做。

    Install-Package Microsoft.AspNet.FriendlyUrls
    

    然后在您的应用程序中添加 Global.asax 文件

    void Application_Start(object sender, EventArgs e)
    {
    RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
    }
    

    测试您的应用程序,您会看到 .aspx 扩展名已被删除。

    【讨论】:

    【解决方案2】:

    我终于解决了从文件中删除 .aspx 扩展名的问题。 我希望我的 URL 看起来像: http://localhost:58370/ShowPage 而不是 http://localhost:58370/ShowPage.aspx

    1)我在一个名为 ShowPage 的文件夹中添加了 ShowPage.aspx 页面。 2)以下是我添加到我的 web.config 文件中的规则:

     <rewrite>
              <rules>
                <clear />
                <rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
                  <match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                  <action type="Redirect" url="{R:1}" />
                </rule>
                <rule name="RewriteASPX" enabled="true">
                  <match url="(.*)" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="{R:1}.aspx" />
                </rule>
              </rules>
            </rewrite>
    

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2022-03-04
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2014-04-15
      相关资源
      最近更新 更多