ASP.NET中的URL重写将http://example.com/abcd转换为http://example.com/page.aspx?id=abcd

时间:2012-07-27 21:15:09

标签: asp.net

我在我的网络表单ASP.NET应用程序中遇到问题,我想从

重写网址

http://example.com/abcd

进入

http://example.com/page.aspx?id=abcd

  • abcd 部分将是唯一的,我无法为其创建文件夹
  • 我希望用户始终能够看到 http://example.com/abcd 网址
  • Windows Azure中的解决方案是否相同?

有人可以帮我提一些提示吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

在你的web.config中,在system.webServer部分输入如下内容:

<!-- This has been added to support url rewriting for ... -->
<rewrite>
  <rules>
    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
      <match url="^Page\.aspx$" />
      <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^id=([a-zA-Z]+)$" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="^([a-zA-Z]+)$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="Page.aspx?id={R:1}" />
    </rule>
  </rules>
</rewrite>

请注意,我已从生产网站中的配置中复制并进行了一些修改......需要测试。

当您只有单词([a-zA-Z] +)时,配置应该有效,更改模式以使其适用于数字。

希望有所帮助