C# 无法将json对象作为字符串返回
我有一个返回字符串数据类型的C#函数。我想返回一个json对象作为字符串,但我得到了这个错误:CS1002:;预期 以下是功能:C# 无法将json对象作为字符串返回,c#,json,c#-4.0,C#,Json,C# 4.0,我有一个返回字符串数据类型的C#函数。我想返回一个json对象作为字符串,但我得到了这个错误:CS1002:;预期 以下是功能: public String LoginUser(string Username, string Password) { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; SqlCo
public String LoginUser(string Username, string Password)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
try
{
SqlCommand cmd = new SqlCommand("Login_User");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
return "{"message": "Successfully"}"; //me sukses
}
else
return "{"message": "Invalid Username and/or Password"}"; //pas sukses
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con.State != ConnectionState.Closed)
{
con.Close();
}
}
}
据我所知,你需要在两个字符串作为
return "{\"message\": \"Successfully\"}"; //me sukses
及
或者使用逐字字符串文字,就像将它们加倍一样
return @"{""message"": ""Invalid Username and/or Password""}";
及
用于自动处理连接、命令和读卡器,而不是手动调用Close
或dispose
方法
不要使用AddWithValue
方法。使用Add
方法重载指定参数类型及其大小
我强烈怀疑你把密码当作明文保存不要那样做。阅读据我所知,您需要在两个字符串中都显示为
return "{\"message\": \"Successfully\"}"; //me sukses
及
或者使用逐字字符串文字,就像将它们加倍一样
return @"{""message"": ""Invalid Username and/or Password""}";
及
用于自动处理连接、命令和读卡器,而不是手动调用Close
或dispose
方法
不要使用AddWithValue
方法。使用Add
方法重载指定参数类型及其大小
我强烈怀疑你把密码当作明文保存不要那样做。阅读在字符串前面使用
@
可以轻松地转义双引号
return @"{"message": "Invalid Username and/or Password"}";
尝试更改:
catch (Exception ex)
{
throw ex; // don't do this
}
致:
在字符串前面使用
@
可以轻松地转义双引号
return @"{"message": "Invalid Username and/or Password"}";
尝试更改:
catch (Exception ex)
{
throw ex; // don't do this
}
致:
但当我使用JQuery使用此方法时,浏览器中的响应格式显示为斜杠/。当返回到浏览器中时,我想从对象中删除斜杠!!!我该怎么做?我想返回不带\的结果。当返回到浏览器中时,我想删除所有/来自对象!!!我该怎么做呢?但当我使用JQuery这个方法时,浏览器中的响应格式显示为斜杠/。当返回到浏览器中时,我想从对象中删除斜杠!!!我该怎么做?我想返回不带\的结果。当返回到浏览器中时,我想删除所有/来自对象!!!我该怎么做?