伪静态我之前有试过,就是在<httpHandlers>这一次加入filter,然后根据自己的需要把 .aspx?id=1 转为 1.html
<httpHandlers>
<add verb="*" path="*/show-?*-?*.html" type="UrlRewriter.urlRewriter"/>
<!--<add verb="*" path="*/html/*.html" type="UrlRewriter.urlRewriter" />-->
<!--<add path="*.*" verb="*" type="FilterDomainName.FilterDomainName" validate="true"/>-->
<!--<add path="index.html" verb="*" type="UrlRewriter.urlRewriter" validate="true"/>-->
<!--<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>-->
</httpHandlers>
namespace UrlRewriter
{
public class urlRewriter : IHttpHandler //实现“IHttpHandler”接口
{
public urlRewriter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void ProcessRequest(HttpContext Context)
{
try
{
//取得原始URL屏蔽掉参数
string Url =Context.Request.RawUrl;
//建立正则表达式
Regex reg = new Regex(@"/show-(\d+)-(\d+)\..+",RegexOptions.IgnoreCase);
//用正则表达式进行匹配
Match m = reg.Match(Url,Url.LastIndexOf("/"));//从最后一个“/”开始匹配
if(m.Success){//匹配成功
string realPath =string.Format(@"/aspx/show.aspx?type={0}&id={1}",m.Groups[1],m.Groups[2]);
//Context.Response.Write(RealPath);
//Context.RewritePath(RealPath);//(RewritePath 用在无 Cookie 会话状态中。)
//realPath = "/urlRewriter"+realPath;
Context.Server.Execute(realPath);
}
reg = new Regex(@"/showitem-(\d+)\..+",RegexOptions.IgnoreCase);
m = reg.Match(Url,Url.LastIndexOf("/"));
if (m.Success)
{//匹配成功
string realPath = string.Format(@"/aspx/show.aspx?type={0}", m.Groups[1]);
//Context.Response.Write(RealPath);
//Context.RewritePath(RealPath);//(RewritePath 用在无 Cookie 会话状态中。)
//realPath = "/urlRewriter"+realPath;
Context.Server.Execute(realPath);
}
//else{
// Context.Response.Redirect(Context.Request.Url.ToString());
//}
}
catch
{
Context.Response.Redirect(Context.Request.Url.ToString());
}
}
/// <summary>
/// 实现“IHttpHandler”接口所必须的成员
/// </summary>
/// <value></value>
public bool IsReusable
{
get { return false; }
}
}
}
这里要注意的是iis的扩展映射问题,注意要配置下
|