2010-09-08 32 views
2

當有人登錄到我的網站時。我想引導他們到他們自己的主頁。如果用戶的ID爲1.他們會去根據用戶數據重定向到url

http://www.test.com/Home.aspx?id=1 

我已經有登錄名和ID設置。我不知道如何將它合併到網址中。

回答

1

您使用的是Forms Authentication嗎?

如果是這樣,而不是使用RedirectFromLoginPage(它將重定向到web.config中的任何頁面),只需使用FormsAuthentication.SetAuthCookie,並執行自己的重定向。

爲此,您需要使用網址QueryString

E.g

// forms auth code here, user is logged in. 
int id = 1; 
string redirectUrlFormat = "http://www.test.com/Home.aspx{0}"; 
string queryStringidFormat = "?id={0}"; 
Response.Redirect(string.Format(redirectUrlFormat, string.Format(queryStringidFormat, id))); 

你應該處理所有的查詢字符串參數,URL等(即上面的代碼)在全球靜態模型類。

這樣,你可以只說:

Response.Redirect(SomeStaticClass.GetUserHomePageUrl(id)); 

在接收頁面(Home.aspx),使用下面的代碼來獲取用戶的ID:

var userId = Request.QueryString["id"]; // again, this "magic string" should be in a static class. 

希望幫助。

3
Response.Redirect("http://www.test.com/Home.aspx?id=" + id);