我遇到的问题是FormatException错误类型的异常:' System.FormatException'发生在mscorlib.dll但未在用户代码中处理 附加信息:Guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。因此,当返回URL被注释掉时Guid会被发现并正在工作但是当我因某种原因包含它时,Guid全部为零。 这是代码
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 :(得分:1)
有效网址必须采用以下格式:
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;
答案 1 :(得分: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);
}