2016-10-18 25 views
0

我遇到的問題是FormatException錯誤在mscorlib.dll中發生類型:'System.FormatException'的異常,但未在用戶代碼中處理 其他信息:Guid應該包含32個數字,並帶有4個破折號(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。因此Guid在返回URL被註釋掉時正在工作,但當我出於某種原因包含Guid時,Guid全部爲零。 這裏是代碼從配置文件頁面登錄時嘗試獲取返回URL

 if (Request.QueryString["userId"] != null) 
     { 
      MasterPage masterpage = Page.Master; 
      HtmlAnchor anchor = (HtmlAnchor)masterpage.FindControl("ancLogin");    


      Guid userId = new Guid(Request.QueryString["userId"]); 
      User user = new User(); 
      user = user.GetById(userId); 
      lblUserName.Text = user.UserName; 
      imgProfile.ImageUrl = "~/" + user.ProfilePic; 

      PostList posts = new PostList(); 
      posts = posts.GetByUserId(userId); 
      CommentsList comments = new CommentsList(); 
      comments = comments.GetByUserId(userId); 
      rptPost.DataSource = posts.List; 
      rptPost.DataBind(); 
      rptComments.DataSource = comments.List; 
      rptComments.DataBind(); 
      anchor.HRef = "/Account/Login.aspx?returnURL=/Account/Profile.aspx?userId=" + userId; 


      if (Session["User"] != null) 
      { 
       if (((User)Session["User"]).Id == userId) 
       { 

        btnChangePicture.Enabled = true; 
        fuChangeProfileImage.Enabled = true; 
        fuChangeProfileImage.Visible = true; 
        btnChangePicture.Visible = true; 
       } 

      } 
     } 

     else 
     { 
      Response.Redirect("Default.aspx"); 
     } 

這是我遇到的時候添加到代碼我得到的格式異常麻煩就行了。 anchor.HRef =「/Account/Login.aspx?returnURL=/Account/Profile.aspx?userId=」+ userId; 此外,這就像我第二次問這裏的問題,讓我知道如果我格式化代碼錯誤或任何感謝!

編輯我修復了這個問題我在登錄頁面中發生了一些其他的連接,並且它添加了userId兩次。感謝大家的幫助。

+0

錯誤消息告訴你什麼是錯誤的:'userId' GUID格式錯誤。 – Steve

+0

這就是爲什麼它是一個奇怪的錯誤,因爲格式是正確的,我使用相同的Guid通過我的存儲過程搜索數據庫,例如這是一個用戶配置文件,它正在加載評論和他們的帖子由同一個userId 。當我將錨點href添加回代碼時,它變爲全零。 – Chase

回答

1

一個有效的URL必須是這種格式:

baseURL?query1=value1&query2=value2&query3=value3 

在你的情況下,更換

anchor.HRef = "/Account/Login.aspx?returnURL=/Account/Profile.aspx?userId=" + userId; 

anchor.HRef = "/Account/Login.aspx?returnURL=/Account/Profile.aspx&userId=" + userId; 
+0

我也試過,但我然後從本地服務器得到一個404錯誤。此外,我還有一個返回網址,與另一個aspx頁面中的 – Chase

+0

完全一樣。 anchor.HRef =「/Account/Login.aspx?returnURL=/ExpandedPost.aspx?postId=」+ postId; < - 是行,它完美的與?代替 &。對不起並不意味着做出這兩個單獨的答覆。 – Chase

+0

404意味着無法找到頁面。大多數情況下,這意味着給定的URL不會指向有效的頁面。 –

0

可能你的guid包含一些不需要的字符。 在添加查詢字符串之前轉換base64中的guid,以及在要使用時從base64轉換回字符串。

您可以使用下面的代碼轉換: -

public static string Base64Encode(string plainText) { 
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); 
    return System.Convert.ToBase64String(plainTextBytes); 
} 

public static string Base64Decode(string base64EncodedData) { 
    var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); 
    return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); 
} 
+0

Guid可以安全地添加到querystring中。它到處都是。無需編碼。 –

相關問題