asp.net伪静态使用UrlRewriter.cs重写,实现了伪静态,Product.aspx?id=4
》》 Product-4.html
这是没问题
但是产品搜索时,ProductSearch.aspx?key=美白
》》重写伪静态后》》 ProductSearch-美白.html
页面无法显示
UrlRewriter.cs代码如下:
public class UR_ProductSearch:IHttpHandler
{
/// <summary>
/// 自定义 HttpHandler
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
try
{
string url = context.Request.RawUrl;//获取用户请求的URL地址信息
Regex Reg = new Regex(@"/ProductSearch-(\d+)\..+", RegexOptions.IgnoreCase);//建立正则表达式
Match m = Reg.Match(url, url.LastIndexOf("/"));//用正则表达式进行URL字符串
if (m.Success)//匹配成功
{
string RealPath = @"~/ProductSearch.aspx?key=" + m.Groups[1];//重定向真实的地址信息
context.Server.Execute(RealPath);
}
else
{
context.Response.Redirect(context.Request.Url.ToString());
}
}
catch (Exception)
{
context.Response.Redirect(context.Request.Url.ToString());
}
}
/// <summary>
/// 如果 System.Web.IHttpHandler 实例可再次使用,则为 true;否则为 false。
/// </summary>
public bool IsReusable
{
get { return false; }
}
}
求可以实现使用汉字的伪静态代码,谢谢
生成汉字文件名时候先将汉字编码,你看网页的URL里如果有汉字一般都会转成一串数字和字母的编码。
在后台里再对编码还原成汉字。