2012-09-22 166 views
0

我想更好地瞭解新的參數設置爲URL 的問題,並通過改變查詢字符串參數/值

var ParaValue = Request.QueryString["parameterName"];

所以檢索它,如果我有一個URL:「HTTP:// WWW .myWebsite.aspx?用戶名=愛麗絲」

我將通過例如檢索它上面

string uName = Request.QueryString["UserName"].ToString();

但是如果我想改變數值例如使用戶名=「拉爾夫」

  • 重新編輯

當按鈕被按下時, 有一個參數的「狀態」,其保持參考至極按鈕被按下 狀態的值= 「none」 現在我想將其設置爲img_button1。

我甚至不發送實際工作imgbutton ID

我努力的編碼它只是用於測試/參考

,所以我可以知道我在事件中由給定事件的procidure reaquested階段的 BUTTON1

img_button2

事件triggerd我然後對子級要設置的狀態爲 「img_button2」 等」

回答

3

我做我的研究(我不能標誌着這裏懇請給我交任何回答)後 然後我測試我在this Stack Overflow Page遇到了兩個選項:

第一個選項(艾哈邁德Mageed給出)我已經測試過,工作得很好。 和可讀性容易理解(因爲我仍然記憶猶新到asp.net「招數」)

再其次 annakata答案這是顯着改善的方式方法,你 實際上並不重定向實現結果 - 查詢字符串玩弄我已經desided遵循annakata的做法 並作出使用也redirerion選項 與修改查詢字符串參數&值的輔助方法,修改後

public void QuerStrModify(string CurrQS_ParamName, string NewQs_paramName, string NewPar_Value, bool redirectWithNewQuerySettings = false) 
{ 

    // reflect to readonly property 
    PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); 

    // make collection editable 
    isReadOnly.SetValue(this.Request.QueryString, false, null); 

    // remove 
    this.Request.QueryString.Remove(CurrQS_ParamName); 

    // modify 
    this.Request.QueryString.Set(NewQs_paramName, NewPar_Value); 

    // make collection readonly again 
    isReadOnly.SetValue(this.Request.QueryString, true, null); 
    string FullUrl = Request.Url.AbsolutePath; 
    if (redirectWithNewQuerySettings) 
    { 
     Response.Redirect(string.Join("?", FullUrl, this.Request.QueryString)); 
    } 

} 

我發現它的人具有與asp.net意識中 所以我張貼我的版本正確答案的,因爲我看到了相當少的經驗非常有幫助。 我希望它能幫助其他人尋求相同的解決方案。

隨時可以進一步改善它,因爲我提到我不是一個成熟的人才..yet。

0

您可以使用HttpModule

public class SimpleRewriter : System.Web.IHttpModule 
{ 

    HttpApplication _application = null; 

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += new System.EventHandler(context_BeginRequest); 
     _application = context; 
    } 

    public void Dispose() 
    { 
    } 

    private void context_BeginRequest(object sender, System.EventArgs e) 
    { 
     string requesturl = 
      _application.Context.Request.Path.Substring(0, 
       _application.Context.Request.Path.LastIndexOf("//") 
      ); 

     string[] parameters = requesturl.Split(new char[] { '/' }); 

     if (parameters.Length > 1) 
     { 
      string firstname = parameters[1]; 
      string lastname = parameters[2]; 


      //Here you can modify your parameters or your url 

      _application.Context.RewritePath("~/unfriendly.aspx?firstname=" + 
       firstname + "&lastname=" + lastname); 

     } 
    } 
} 

鏈接:http://msdn.microsoft.com/en-us/library/ms972974.aspx

註冊:

<configuration> 
    <system.web> 
    <httpModules> 
     <add name="SimpleRewriter" type="SimpleRewriter"/> 
    </httpModules> 
    </system.web> 
</configuration> 

鏈接:http://msdn.microsoft.com/en-us/library/ms227673%28v=vs.100%29.aspx

+0

看起來很沉重,我猜想這件事太簡單了。感謝所有回覆,我現在將對它進行調查! – LoneXcoder

+0

只需將此類添加到您的解決方案,並在web.config中註冊您的模塊,運行應用程序並設置斷點以便分析 –

+0

輕鬆容易我的朋友,這很容易。我可以理解項目內部的新課程,而不是新的項目 - 新的Web表單。 breakPoints就像我的心臟脈衝我一直都在使用它,但是「模塊部分」對我來說是一箇中文術語,而且我通過內部表單來關閉它並不重要,但是作爲'HttpApplication context'提供的內容' – LoneXcoder

0

這裏的問題是 「真理的來源」 的問題。 HttpRequest.QueryString公開的NameValueCollection公開查詢字符串,因此不應修改,因爲查詢字符串是由調用者提供的。如果應用程序具有用戶名查詢字符串參數,但可能需要更改(例如進行測試),則可以將其包裝在一個方法中,以便根據需要將其替換爲備用來源。例如:

// A very simple container 
public static class SystemInfo 
{ 
    // This would be an instance of QueryStringUserInfo 
    // by default but could be changed for testing. 
    public IUserInfo UserInfo 
    { 
     get; 
     private set; 
    } 
} 

// An interface that contains the user operations 
public interface IUserInfo 
{ 
    string UserName { get; } 
} 

// Get the user name from the query string. This would 
// be the default. 
public class QueryStringUserInfo: IUserInfo 
{ 
    public string UserName 
    { 
     get 
     { 
      return Request.QueryString["UserName"].ToString(); 
     } 
    } 
} 

// Get the user name from the query string. This would 
// be the default. 
public class TestUserInfo: IUserInfo 
{ 
    public string UserName 
    { 
     get 
     { 
      return "Foo"; 
     } 
    } 
} 

因此,而不是直接調用查詢字符串爲用戶名(或任何資料片),呼籲系統的系統信息的屬性(可怕的名字,但你明白了吧)。它允許更改設置的來源,例如代碼是在網頁外部使用還是用於測試。

+0

我給它作爲一個簡單的例子,如果我沒有真正訪問一個用戶名的參數。事實是,我試圖測試並顯示價值,並根據通過事件或條件改變的價值做出決定。生病嘗試改進我的問題... – LoneXcoder

+0

「將其包裹在一個方法中」 - 部分,你可以給出一個更簡單的例子,更容易實現嗎?我可以使用@Yakoub建議的類,但它看起來非常複雜,至少對於我來說,作爲一個新手,有沒有可能簡化它呢? – LoneXcoder

+0

@LoneXcoder我已經添加了一個簡單的例子來演示這個概念。 – akton