這聽起來像問題是你想讓每個用戶都有自己的網址......是這樣嗎?
您可以使用URL重寫。其中一種更簡單的方法是在Global.asax
文件的Application_BeginRequest
方法中使用HttpContext.RewritePath。
編輯:
我在這裏做一些假設加樣品代碼...我想提出的假設是:a)用戶有一個名字,B)用戶有一個姓氏和c)用戶有一些獨特的標識符,在我的情況下,我正在使用「userId」。
首先,您需要決定公共網址的格式。像這樣的東西可以工作:
http://www.yoursite.com/public/firstname-lastname-1234.aspx
其中1234是userId。
接下來,您將需要一個函數來構建這些URL。也許這樣的東西:
String ToPublicUrl(String firstName, String lastName, int userId)
{
return String.Format("http://www.yoursite.com/public/{0}-{1}-{2}.aspx",
Regex.Replace(firstName, "[^a-zA-Z]", ""),
Regex.Replace(lastName, "[^a-zA-Z]", ""),
userId);
}
請注意我是如何剝離出任何非字母字符。
現在,在Global.asax文件,添加以下代碼:
static Regex ProfileRegex = new Regex(@"/public/(?<firstname>[a-zA-Z]+)-(?<lastname>[a-zA-Z]+)-(?<userid>[0-9]+)\.aspx$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
void Application_BeginRequest(object sender, EventArgs e)
{
Match match = ProfileRegex.Match(Context.Request.FilePath);
if((match != null) && match.Success)
{
Context.RewritePath((String.Format("~/public/userProfile.aspx?userId={0}",
match.Groups["userid"]));
}
}
有很多東西會在這裏的。首先,通過簡單的定義這個函數,ASP.Net將把它訂閱到HttpApplication.BeginRequest事件。
接下來是正確的正則表達式。它基本上匹配對用戶個人資料頁面的任何請求,並在內部告訴ASP.Net將請求路由到另一個頁面。新的請求將是這個樣子:
http://www.yoursite.com/public/userProfile.aspx?userId=1234
它是由你來實現/public/userProfile.aspx
頁。有很多方法可以使事物只讀。它可能是直接替代TextBox控件與Label控件?
如果您有多個要公開的頁面,則可以實現多個重寫規則。 (雖然在某些時候你最好使用像this或this這樣的組件)。
作爲供參考,我參考this page的樣品進行重寫。如果您有任何問題,請告訴我。
非常感謝。這讓我更好地瞭解正在發生的事情以及我需要做什麼來創建該網址。看來只讀應該不是一個大問題。我會在本週末嘗試一下,再次感謝。 – 2010-12-16 20:50:11
沒問題...祝你好運! – dana 2010-12-17 05:17:45
Dana,何時以及如何使用ToPublicUrl()?謝謝。 – 2010-12-30 03:20:41