获取重写请求的URL

本文关键字:URL 请求 重写 获取 | 更新日期: 2023-09-27 18:10:52

我网站上的所有用户都有面向公众的个人资料页面。我正在使用URL重写将URL从形式http://mysite.com/profile.aspx?id=sdsdfsdsdfsdfdsdffsdfsdf更改为http://mysite.com/Username,就像这样(在我的全局。asax文件):

static Regex _handleRegex1 = new Regex("/(?<hndl>[''w]+)/?$", RegexOptions.Compiled);
void Application_BeginRequest(object sender, EventArgs e)
{
    System.Text.RegularExpressions.Match handleMatch = _handleRegex1.Match(Request.Url.LocalPath);
    if(handleMatch.Success){
        String handle = handleMatch.Groups[1].Value;
        using (SqlQuery query = new SqlQuery("[dbo].[sp_getUserIdByHandle]"))
        {
            try
            {
                query.AddParameter("@handle", handle, System.Data.SqlDbType.NVarChar, false);
                query.AddParameter("@userId", new Guid(), System.Data.SqlDbType.UniqueIdentifier, true);
                query.ExecuteNonQuery();
                Object userId = query.GetOutParameter("@userId");
                if (userId == DBNull.Value)
                {
                    Response.Redirect("~/default.aspx");
                }
                else
                {
                    Context.RewritePath(string.Format("~/profile.aspx?id={0}&{1}", userId, Request.QueryString));
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

这很好。但是,如果我对服务器进行回发,URL就会从/username这样的形式变为/profile?id=5ab47aa3-3b4d-4de6-85df-67527c9cdb52&,我想对用户隐藏它。

我想做一些像Response.Redirect(Request.RawUrl);这样的事情来把用户送回正确的页面。然而,Request似乎没有包含任何关于所需URL的信息。

是否有办法找到预先重写的URL?

获取重写请求的URL

你在哪个平台?如果是IIS 7,你最好使用IIS URL重写2.0。

我建议你使用Routing,这样你就可以写你的代码ASP。. NET方式,但仍然有你想要的url:

使用ASP路由. NET Web Forms