在Rewrite Rule Web.config中为特定页面附加查询字符串

时间:2015-10-13 14:29:20

标签: asp.net url-rewriting web-config rewrite

我想添加打开以下页面 http://test.com/Collection.aspx?title=Women

as

http://tests.com/Women

http://tests.com/Collection.aspx?title=Women

作为

http://test.com/pathann

我尝试使用下面的重写规则,但这些适用于所有页面,我只想实现此特定部分。

 <rule name="RedirectUserFriendlsssyURL1" stopProcessing="true">
      <match url="^Collection\.aspx$" />
      <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^title=([^=&amp;]+)$" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" />
    </rule>
      <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="Collection.aspx?title={R:1}" />
    </rule>

请帮助我如何才能为这些特定页面执行此操作。实际上,如果我申请所有页面,它会阻止其他一些功能。

1 个答案:

答案 0 :(得分:1)

在您的情况下,我要做的是只将重定向部分留给网址重写模块:

<rule name="RedirectUserFriendlsssyURL1" stopProcessing="true">
   <match url="^Collection\.aspx$" />
   <conditions>
     <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
     <add input="{QUERY_STRING}" pattern="^title=([^=&amp;]+)$" />
   </conditions>
   <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

通过在global.asax:

中路由来处理其余部分
void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("CollectionRoute",
        "{title}",
        "~/Collection.aspx", false,
        new RouteValueDictionary(),
        //Here you can define the regex pattern to match the title phrases
        new RouteValueDictionary {
            { "title", "(women)|(pathann)" }
        });
}

但是当然如果您仍然希望保留由url rewrite模块处理的所有内容,您可以像这样定义规则:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="(women)|(pathann)" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="Collection.aspx?title={R:1}" />
</rule>