【问题标题】:Rewriting URLs in ASP.NET/C#在 ASP.NET/C# 中重写 URL
【发布时间】:2012-03-18 17:47:35
【问题描述】:

如何使用以下代码调用帐户

http://www.domain.ext/madcoder

而不是

http://www.domain.ext/index.aspx?key=madcoder

因为我的madcoder 是我获取数据库的主要搜索键

我找到了以下代码,但不明白如何使用它。请任何人帮助我。

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
  <match url="^([^/]+)/?$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Rewrite" url="?p={R:1}" />
</rule>

编辑 1

我尝试以下面的方式修改我的 web.config 文件,这给了我错误

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
        <match url="^([^/]+)/?$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="index.aspx?key={R:1}" />
    </rule>
</configuration>

【问题讨论】:

    标签: asp.net url


    【解决方案1】:

    如果您使用的是 .net 4.0,它会容易得多。

    .net 4 Webforms Routing

    【讨论】:

      【解决方案2】:

      怎么样:

      <action type="Rewrite" url="index.aspx?key={R:1}" />
      

      关于匹配/替换标签:match-url 应用于 url 之后 域斜杠(在您的情况下,在 http://www.domain.ext/ 之后)。括号用于分组和捕获,因此([^/]+) 将匹配任何不包含斜线的内容,就在域之后。它可能包含尾部斜杠,但目前不允许使用其他任何内容。

      重写操作可以包含文字字符串,但也可以包含{R:xxx} 引用,它引用您之前在括号中捕获的任何内容。在这种情况下,字符串madcoder

      【讨论】:

      • 我应该在哪里写这个动作代码。是在 web.config 文件还是任何其他文件中?
      • @Mad coder:这些规则进入 web.config。您可以直接在 IIS 中最轻松地编辑它们,只需单击 URL 重写图标。它有一个 GUI,让编辑(和测试!)重写规则变得轻而易举。
      【解决方案3】:

      试试这个成功 在 web config 中编写以下代码

       <system.web>
              <urlMappings enabled="true">
                <add url="~/Index" mappedUrl="Index.aspx?key={R:1}"/>
      
              </urlMappings>
          </system.web>
      

      在c#中编写如下代码

      Response.redirect("~/index")
      

      否则你也可以传递你的价值

      【讨论】:

        猜你喜欢
        • 2011-10-23
        • 2011-08-02
        • 2010-10-05
        • 2011-08-03
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-28
        • 2011-09-30
        相关资源
        最近更新 更多