各位好,小弟正在学习url重写,在网上看了那些方法好像都不是很全面,而且大多数是一样,不知道各位有做过这方面的没有,发一份我参考一下,谢谢了!
解决方案 »
- asp:GridView如何編輯和傳遞
- cs里面不能识别div的id ?
- document.form.sumbit 方法提交失败~
- 自定义Web控件,加载时用LoadControl()与new()的区别?
- JPE, JPEG,JPG 是一个意思吧,都属于类型 “image/pjpeg” ???
- 请问哪里有SQLHelper.cs文件的下载?
- 谁使用过Infragistics.WebUI.UltraWebTab控件,实现象QQ打开多个窗口
- 网站名称注册管理暂行办法实施细则>>有人注册过网站名称吗
- 如何在DataGrid中实现超级链接?
- DataGrid中使用Checkbox 的一个问题
- ASP之多个单选按钮组的问题???
- GridView和DropDownList的问题。会的来看看啊。(急救!)
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;/// <summary>
///URL重写
/// </summary>
public class UrlReWriter:IHttpModule
{
public UrlReWriter(){}
public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Response.Write("<html>");
context.Response.Write("<head><title>出错了!</title></head>");
context.Response.Write("<body style=\"font-size:14px;\">");
context.Response.Write("出错了:<br />");
context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</textarea>");
context.Response.Write("</body></html>");
context.Response.End();
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path); string oldfile = Regex.Replace(file, @"\d+", ""); Regex[] regexs = new Regex[]
{
new Regex("djyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("wlyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("glmj(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxzx(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxpc(\\d+).aspx", RegexOptions.Compiled),
new Regex("CreativeFun(\\d+).aspx", RegexOptions.Compiled),
new Regex("News(\\d+).aspx", RegexOptions.Compiled)
}; foreach (Regex rex in regexs)
{
Match match = rex.Match(file); if (match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = oldfile+ "?index=" + userId;
context.RewritePath(rewritePath); break;
}
}
}}
关键就是将URL截取 然后跳转
下面的 重写 可能好点把