使用 URLRewriter.NET 重写可包含 1 或 2 个查询字符串的 Url?

发布于 2024-07-14 09:42:09 字数 1031 浏览 5 评论 0原文

在我的项目中,我的/PropertDetail.aspx可以获得2个查询字符串。

第 1 一个用于 PropertyId /PropertDetail.aspx?PropertyId=5

2nd 一个用于语言 /PropertDetail.aspx?PropertyId=5& ;Language=2

编辑: 并且此页面可以获取其中之一,也可以获取两者,因此我的重写器规则需要处理它们两者

所以,我已将这些规则设置为web.config

<rewriter>
        <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop" />
        <rewrite url="^/(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2" processing="stop"/>
        <!--http://localhost:1562/Harika-Gayrimenkul-5.aspx-->
        <rewrite url="^/(.+)-(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2&#038;Language=$3" processing="stop"/>
        <!--http://localhost:1562/Great-Property-5-2.aspx-->
</rewriter>

如果没有语言查询字符串,那么一切都可以,但是当有语言查询字符串时,它会获取第三个表达式作为 PropertyId 而不是 Language

我如何定义这两个规则适用于同一页面吗?

谢谢

In my project, my /PropertDetail.aspx can get 2 querystrings.

1st one for the PropertyId /PropertDetail.aspx?PropertyId=5

2nd one for the Language /PropertDetail.aspx?PropertyId=5&Language=2

EDIT: and this page can get one of them or can get both of them, so my rewriter rule needs to handle both of them

So, i have set these rules to web.config

<rewriter>
        <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop" />
        <rewrite url="^/(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2" processing="stop"/>
        <!--http://localhost:1562/Harika-Gayrimenkul-5.aspx-->
        <rewrite url="^/(.+)-(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2&Language=$3" processing="stop"/>
        <!--http://localhost:1562/Great-Property-5-2.aspx-->
</rewriter>

It is all OK if there is no Language querystring, but when there is a language querystring it gets the 3rd expression as the PropertyId instead of Language

How can i define these two rules for the same page ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情未る 2024-07-21 09:42:09

组合答案:

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>

</rewriter>

现在对于许多组合来说效果都很好:

/This-is-a-really-long-property-title-555-12

返回 PropertyId=555 和 Language=12。

/This-is-another-really-long-property-title-666

返回 PropertyId=666。

Combined answer:

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>

</rewriter>

That's working well now for many combinations:

/This-is-a-really-long-property-title-555-12

returns PropertyId=555 and Language=12.

/This-is-another-really-long-property-title-666

returns PropertyId=666.

别念他 2024-07-21 09:42:09

通过添加问号,使第二个参数(语言值)在匹配中可选:

编辑:这是一个更正的版本,是在我意识到我有点误解了问题后制作的。

<rewriter>
  <rewrite url="\.(?:gif|png|jpg|ico|pdf|css|js)(?:\?.*)?$" to="$0" processing="stop"/>
  <rewrite url="(\d+)(?:-?(\d+)?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
</rewriter>

这是一个干净的版本OP 正在运行的内容的升级和简化版本。 它将产生以下形式的输出:

/PropertyDetail.aspx?PropertyId=12345&Language=1   (when language is present)
/PropertyDetail.aspx?PropertyId=12345&Language=    (when it isn't)

注意

  • 使用 $0 反向引用来引用整个输入字符串,而无需
  • 使用非捕获组实际匹配整个输入字符串( ?:...) 对于我们不需要存储在匹配组中的内容,因为我们不想稍后检索它们的值将
  • 单参数 URL 和双参数 URL 的单独规则折叠成单个规则

原始版本答案:

<rewriter>
  <rewrite url="^/(.+?)-(.+?)-?(.+?)?\.aspx$" to="/PropertyDetail.aspx?PropertyId=$2&#038;Language=$3" processing="stop"/>
</rewriter>

Make the second parameter (the language value) optional in the match by adding a question mark:

Edit: this is a corrected version, made after I realized that I misunderstood the question a bit.

<rewriter>
  <rewrite url="\.(?:gif|png|jpg|ico|pdf|css|js)(?:\?.*)?$" to="$0" processing="stop"/>
  <rewrite url="(\d+)(?:-?(\d+)?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
</rewriter>

This is a cleaned-up and streamlined version of what the OP has working. It will produce output in the form of

/PropertyDetail.aspx?PropertyId=12345&Language=1   (when language is present)
/PropertyDetail.aspx?PropertyId=12345&Language=    (when it isn't)

Note

  • the use of the $0 back-reference to refer to the entire input string, without the need to actually match the entire input string
  • the use of non-capturing groups (?:...) for things we don't need to store in a match-group because we don't want to retrieve their value later
  • the collapse of the separate rules for single and double-argument URLs into a single rule

Original version of the answer:

<rewriter>
  <rewrite url="^/(.+?)-(.+?)-?(.+?)?\.aspx$" to="/PropertyDetail.aspx?PropertyId=$2&#038;Language=$3" processing="stop"/>
</rewriter>
旧伤还要旧人安 2024-07-21 09:42:09

这是我们提出的最终解决方案。

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
</rewriter>
  • 第一条规则是关于文件类型的
    我们不需要。
  • 第二条规则是关于“如果页面获得 2
    querystring”
  • 第三条规则是关于页面是否获得
    只有一个查询字符串

非常感谢 teknohippyJasonMArcher 的帮助

This is the final solution that we have come up with.

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
</rewriter>
  • 1st rule is about the file types
    which we don't need.
  • 2nd rule is about "if page gets 2
    querystring"
  • 3rd rule is about if the page get
    only one querystring

Thank you very much for your helps teknohippy and JasonMArcher

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文