比方說,我們跟蹤我們網站中的20個查詢字符串參數。每個請求將只有這20個參數的一個子集。但我們肯定會查找每個請求中包含的所有/大部分參數。查詢字符串到具有強類型屬性的對象
我們不希望每次在代碼中尋找某個特定的參數時都不想循環收集這些集合。因此,我們通過查詢字符串集合循環一次,將字符串值轉換爲它們各自的類型(枚舉,int,字符串等),填充到添加到上下文的QueryString對象。
之後,在需要的地方,我們將在QueryString對象中有一個強類型的屬性,這很容易使用,我們維護一個標準。
public class QueryString
{
public int Key1{ get; private set; }
public SomeType Key2{ get; private set; }
private QueryString() { }
public static QueryString GetQueryString()
{
QueryString l_QS = new QueryString();
foreach (string l_Key in HttpContext.Current.Request.QueryString.AllKeys)
{
switch (l_Key)
{
case "key1":
l_QS.Key1= DoSomething(l_Key, HttpContext.Current.Request.QueryString[l_Key]);
break;
case "key2":
l_QS.Key2 = DoAnotherThing(l_Key, HttpContext.Current.Request.QueryString[l_Key]);
break;
}
}
return l_QS;
}
}
任何其他解決方案來實現這一目標?
我沒有使用ASP.NET MVC。 – Kamar 2010-06-07 20:05:39