我想实现
请求 http://localhost/comment/007/abc123.html
重写到 http://localhost/comment.aspx?sid=007&tid=abc123我试着这么做,但不成功。希望大家给点意见,我对正则表达式不熟。
<RewriterRule>
<LookFor>~/comment/(\d+)\/^\w+$.html</LookFor>
<SendTo>~/comment.aspx?sid=$1;tid=$2</SendTo>
</RewriterRule>
请求 http://localhost/comment/007/abc123.html
重写到 http://localhost/comment.aspx?sid=007&tid=abc123我试着这么做,但不成功。希望大家给点意见,我对正则表达式不熟。
<RewriterRule>
<LookFor>~/comment/(\d+)\/^\w+$.html</LookFor>
<SendTo>~/comment.aspx?sid=$1;tid=$2</SendTo>
</RewriterRule>
ReplaceTo: /comment.aspx?sid=$1&tid=$2
/comment.aspx?sid=$1&tid=$2 在web.config 里有错误,提示应为“;”
/comment.aspx?sid=$1;tid=$2
<LookFor>~/comment/([^/]*)/([^.]*).html</LookFor>
<SendTo>~/t2.aspx?rid=$1;tid=$2</SendTo>
</RewriterRule><asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/comment/007/abc123.html">link</asp:LinkButton>public partial class t2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["rid"] != null)
{
Response.Write(Request.QueryString["rid"].ToString());
}
if (Request.QueryString["tid"] != null)
{
Response.Write(Request.QueryString["tid"].ToString());
}
}
}
其结果输出的是 007;tid=abc123
好像参数tid 没有用
SRC: /comment/([^/]*)/([^.]*).html
ReplaceTo: /comment.aspx?sid=$1&tid=$2
试试
在这种情况下,如果用户访问 /2004/02/14.aspx,我们需要将 URL 重写为 ShowBlogContent.aspx?year=2004&month=2&day=14。所有三种情况(URL 指定了年、月和日时;URL 仅指定了年和月时;URL 仅指定了年时)均可使用重写规则进行处理:<RewriterConfig>
<Rules>
<!-- Blog 内容显示程序规则 -->
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/Default\.aspx</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>这些重写规则表明了正则表达式的功能。在第一个规则中,我们使用模式 (\d{4})/(\d{2})/(\d{2})\.aspx 查找 URL。在简明英语中,它对应了这样一个字符串:首先是四个数字,后跟一个斜杠,然后是两个数字,后跟一个斜杠,然后再跟两个数字,最后是一个 .aspx。每个数字组周围的括号非常重要,通过它可以在相应的 <SendTo> 属性中引用这些括号内的匹配字符。 特别是,我们可以针对第一、第二和第三个括号组分别使用 $1、$2 和 $3 引用回括号内的匹配组。注意:由于 Web.config 文件采用 XML 格式,但是必须对元素文字部分中的字符(如 &、< 和 >)进行转义。在第一个规则的 <SendTo> 元素中,& 被转义为 &。在第二个规则的 <SendTo> 中使用了另外一种技术(使用 <![CDATA[...]]> 元素),无需对内部的内容进行转义。可以使用两种方法中的任何一种,并且都会得到相同的结果。