现在我在做一个博客,想做URL映射,让地址显示为http://localhost/blog/userName
但我现在真实地址是这样的
http://localhost/blog/index.aspx?id=***
我要怎么做才能把 这个id号转换成用户名,然后在映射成我的要URL?userName在数据库中!

解决方案 »

  1.   

    如果已经会Url重写了,直接查数据库再重写就可以了.
    或者不转换,在index.aspx中查一下用户名也可以.
      

  2.   

    cpp2017(慕白兄) 可以在说详细一点吗?
    我只会把URL重写成http://localhost/blog/*.aspx
    这类式的样子,
    就是不知道要怎么把对应id的UserName重写
      

  3.   

    搜索一下利用httpmodule进行重写.HttpContext.Current.RewritePath("aaa.aspx?id="+sId);
      

  4.   

    已经有username了还需要id吗,直接用正则取到username,然后sql就好了
      

  5.   

    恩,主要就是自己写个httpmodule
    拦截请求,解析request 的url
    然后重新转向就可以了~~
      

  6.   

    这里有一种最常用的方法
    http://localhost/blog/index.aspx?id=***
    http://localhost/blog/index_***.html
      

  7.   

    net2.0框价中有
    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;
           }
       }}
      

  8.   

    -_-b
    之前偶是直接用微软的哪个重写的dll,
    现在偶找了别人写的URLRewriter源码来改,但在里面设了断点,程序压根就不经过里面。
    不知道要怎么做:(
      

  9.   

    上面我写的有误:改为:
    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;}
      

  10.   

    呵呵  前两个我也学了下URL重写  我的问题和你的一样 已经解决了  不过我的是Apache
      

  11.   

    菜鸟一问:是不是要实现URL重写,必须在服务器上做动作,不在服务器上做任何事情,可以实现URL重写吗??如果可以,哪位大哥能给个实例??
      

  12.   

    菜鸟一问:是不是要实现URL重写,必须在服务器上做动作,不在服务器上做任何事情,可以实现URL重写吗??如果可以,哪位大哥能给个实例??
    -----------------------------------------------------------
    重写不用设置服务器,如果要把.aspx映射成.html的才用设置服务器明天结帖,今天在试一天!
      

  13.   

    你就在index页面中根据用户名查出ID就可以了