2013-10-25 44 views
0

所以我有這個代碼在我的CS頁面上,需要從我的Url字符串中解碼一個鍵。關鍵是「Reauth_URL」,它的鏈接以base64解碼爲UTF8。base64解碼URL上的空引用excletion

////base 64 decoding for Reauth_URL key in URL query string 
    string encodedString =  Convert.ToString(HttpContext.Current.Request.Params["Reauth_URL"]).Trim(')'); 
    byte[] data = Convert.FromBase64String(encodedString); 
    string decodedString = Encoding.UTF8.GetString(data); 

我試圖使用decodedString但我一直得到一個空refence例外,但我可以看到鍵和值在那裏。

一旦我可以返回字符串值id就像能夠將它發送到我的aspx頁面上的超鏈接。

編碼的網址是從IronPort設置的,它允許用戶在不同用戶的情況下登錄,如果他們被阻止訪問網站。因此查詢字符串中的這個reauth_url鍵允許它們以不同的用戶身份登錄。由reauth_url需要解碼並鏈接到超鏈接。我知道關鍵和價值在那裏,但我無法得到這個空的異常,當我說我知道他們在那裏顯然我不是在上面的代碼中,ive不得不拆分url查詢?和&並將其打印出來並存在。下面的代碼被使用較早,我需要的關鍵和價值在那裏。

string currentUrl = HttpContext.Current.Request.Url.Query; 
    txtBlockedUrl.Visible = true; 
    string [] result = currentUrl.Split(new Char[]{'?','&'}); 

    foreach (string r in result) 
    { 
     txtBlockedUrl.Text += HttpUtility.UrlDecode(r) + "\n"; 
    } 

div style="font-size: medium"> 
    <a href="<%=decodedString%>" style="text-decoration: none; border-bottom: 1px dotted blue;">LogIn as Different User</a> 
</div> 
+0

如果調試一步一步通過這段代碼,你必須看到它發生在哪一行。 – joe

+0

就在第一行詢問reauth_url的參數已經爲空,但是如果對一個通用的標籤進行request.params ['reuth_url']彈出調試,然後將其放入解碼器網站並粘貼編碼的字符串和解碼它解碼後的url顯示出來 – KBriz

回答

0

結束了做這個....

//splitting url string for textbox using name value collection 
    NameValueCollection collection = new NameValueCollection(); 
    string currentUrl = HttpContext.Current.Request.Url.Query; 
    string [] result = currentUrl.Split('&'); 
    foreach (string r in result) 
    { 
     string[] parts = HttpUtility.UrlDecode(r).Split('='); 
     if (parts.Length > 0) 
     { 
      string key = parts[0].Trim(new char[] { '?', ' ' }); 
      string val = parts[1].Trim(); 

      collection.Add(key, val); 
     } 
    } 
0

如果HttpContext.Current.Request.Params["Reauth_URL"]爲null,Convert.ToString將拋出一個空引用異常。

請注意,當「Reauth_URL」不可用時,Params indexer將返回null
所以,你必須首先要檢查它是否存在:(?如果什麼網址不提供的話)

string value = HttpContext.Current.Request.Params["Reauth_URL"]; 
if (value!=null) { 
    string encodedString = Convert.ToString(HttpContext.Current.Request.Params["Reauth_URL"]).Trim(')'); 
    //...