我網站上的所有用戶都有公開的個人資料頁面。我使用的URL重寫從形式http://mysite.com/profile.aspx?id=sdsdfsdsdfsdfdsdffsdfsdf
更改網址爲http://mysite.com/Username
像這樣(我在Global.asax文件):獲取重寫請求的URL?
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的任何信息。
有什麼方法可以找到預先重寫的網址嗎?
仍然工作在服務器2003/IIS6,很遺憾。 – Oliver
文章中的某些建議和鏈接頁面可能仍然可以用於IIS 6 –