积极答复者
关于重写URL

问题
-
msdn上的例子web.config里面的:
<RewriterRule>
<LookFor>~/Products/Default\.aspx</LookFor>
<SendTo>~/ListCategories.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Beverages\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Condiments\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Confections\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Dairy\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=4</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/GrainsCereals\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=5</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/MeatPoultry\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=6</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Produce\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=7</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Seafood\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=8</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
如果想把CategoryID改成所有的数字呢以便自己使用。请问下可以在这里改吗
答案
-
你好 比如
<RewriterRule>
<LookFor>~/Products/Category/(\d)\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=$1</SendTo>
</RewriterRule>
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 Xhp 2009年10月12日 2:46
-
你好\d代表数字 这里更合理的写法是下面的,因为数字可能不止一位
<RewriterRule>
<LookFor>~/Products/Category/(\d+)\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=$1</SendTo> </RewriterRule>
这样就是把那里的数字捕获到组1中
然后~/ListProductsByCategory.aspx?CategoryID=$1中的$1是获取捕获组组1的值
如果改成htm那么就会跳转到htm那个也没,如果不存在这个也没 就会那样显示了
url重写的url可能并不存在,但是后边的SendTo要重写到的url必须是存在的
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 Xhp 2009年10月12日 2:48
- 已编辑 Raymond TangModerator 2009年10月12日 2:58 正则修改
全部回复
-
你好 比如
<RewriterRule>
<LookFor>~/Products/Category/(\d)\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=$1</SendTo>
</RewriterRule>
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 Xhp 2009年10月12日 2:46
-
<RewriterRule>
<LookFor>~/Products/Category/(\d)\.aspx</LookFor> \d是什么意思?能否解释下谢谢。。
<SendTo>~/ListProductsByCategory.aspx?CategoryID=$1</SendTo> 下面的$号呢。。
</RewriterRule>
还有为什么改成htm
<RewriterRule>
<LookFor>~/Default\.aspx</LookFor>
<SendTo>~/ListCategories.htm</SendTo>
</RewriterRule>
就会出现找不到网页
啊 -
你好\d代表数字 这里更合理的写法是下面的,因为数字可能不止一位
<RewriterRule>
<LookFor>~/Products/Category/(\d+)\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=$1</SendTo> </RewriterRule>
这样就是把那里的数字捕获到组1中
然后~/ListProductsByCategory.aspx?CategoryID=$1中的$1是获取捕获组组1的值
如果改成htm那么就会跳转到htm那个也没,如果不存在这个也没 就会那样显示了
url重写的url可能并不存在,但是后边的SendTo要重写到的url必须是存在的
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已标记为答案 Xhp 2009年10月12日 2:48
- 已编辑 Raymond TangModerator 2009年10月12日 2:58 正则修改
-
这里一样的道理哈\d{n}就是指匹配有n位数的数字
>~/(\d{4})/(\d{2})/(\d{2})\.aspx对于这里就是把(\d{4})捕获组设置为组1用$1获得值,一次类推
正则表达式参考 http://dotnet.cnblogs.com/page/47948
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond