现在我在做一个博客,想做URL映射,让地址显示为http://localhost/blog/userName
但我现在真实地址是这样的
http://localhost/blog/index.aspx?id=***
我要怎么做才能把 这个id号转换成用户名,然后在映射成我的要URL?userName在数据库中!
但我现在真实地址是这样的
http://localhost/blog/index.aspx?id=***
我要怎么做才能把 这个id号转换成用户名,然后在映射成我的要URL?userName在数据库中!
或者不转换,在index.aspx中查一下用户名也可以.
我只会把URL重写成http://localhost/blog/*.aspx
这类式的样子,
就是不知道要怎么把对应id的UserName重写
拦截请求,解析request 的url
然后重新转向就可以了~~
http://localhost/blog/index.aspx?id=***
http://localhost/blog/index_***.html
System.Net.HttpListener
System.Net.HttpListenerRequest
两个类
在web.config <httpModules>中添加
<httpModules>
<add name="HttpListenerModule" type="portal.HttpListenerModule"/>
</httpModules>
namespace portal
{
/// <summary>
/// HttpListenerModule的摘要说明 /// </summary>
public class HttpListenerModule:IHttpModule
{
// IHttpModule::Init
public void Init(HttpApplication app)
{
// 注册管道事件
app.AcquireRequestState +=
new EventHandler(OnAcquireRequestState);
} // IHttpModule::Dispose
public void Dispose() {} // 确定是否正在处理 F5 或后退/前进操作
private void OnAcquireRequestState(object sender, EventArgs e) {
// 访问 HTTP 上下文
HttpListener listener = new HttpListener();
HttpListenerRequest request = listener.GetContext().Request;
Uri url = request.url;
//下面就是解析当前的url了应该比较简单我就不说了; return;
}
}}
之前偶是直接用微软的哪个重写的dll,
现在偶找了别人写的URLRewriter源码来改,但在里面设了断点,程序压根就不经过里面。
不知道要怎么做:(
HttpListener listener = new HttpListener(); listener.Prefixes.Add("监听的网页地址");//不过不能监听本机的工程
listener.Start(); HttpListenerRequest request = listener.GetContext().Request;
HttpListenerResponse response = listener.GetContext().Response;
Uri url = request.Url;我重新修改代码弥补这个缺陷:
private void OnAcquireRequestState(object sender, EventArgs e) {
// 访问 HTTP 上下文 HttpApplication app = (HttpApplication) sender;
HttpContext ctx = app.Context;
HttpRequest request = ctx.Request;
HttpResponse response = ctx.Response;
Uri url = request.url;
//下面就是解析当前的url了应该比较简单我就不说了;
return;}
-----------------------------------------------------------
重写不用设置服务器,如果要把.aspx映射成.html的才用设置服务器明天结帖,今天在试一天!