[英]How to return HTTP 204 response on successful DELETE with ServiceStack
[英]ServiceStack return response syntax error
我只是快速將ServiceStack升級到4.5.4版,嘗試轉換服務時遇到問題。 基本上,我不能再將sql響應作為字符串返回。 有誰知道正確的語法嗎?
我已經設法更改為新語法,但是無法找到更改以下行的正確方法:
返回新的PostAccountResponse(){message = string.Format(“ {0}”,message.Value};
public object Post(Account.Model.Account request)
{
switch (request.Accounttype)
{
case "dview":
try
{
return dbFactory.Exec(dbCmd =>
{
dbCmd.CommandType = System.Data.CommandType.StoredProcedure;
dbCmd.CommandText = "stored procedure...";
dbCmd.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("@mobile", request.mobile));
MySql.Data.MySqlClient.MySqlParameter message = new MySql.Data.MySqlClient.MySqlParameter("@message", "");
message.Direction = System.Data.ParameterDirection.Output;
dbCmd.Parameters.Add(message);
gender.Direction = System.Data.ParameterDirection.Output;
r = dbCmd.ExecuteReader();
return new PostAccountResponse() { message = string.Format("{0}", message.Value };
});
}
catch (Exception ex)
{
//
}
break;
}
return new object();
}
有人知道如何正確返回我的服務響應嗎? 非常感謝。 我嘗試了以下方法,但還是沒有用:
var rdr = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
var results = new List<Account.Model.PostAccountResponse>();
while (rdr.Read())
{
results.Add(new Account.Model.PostAccountResponse { message = string.Format("{0}", message.Value) });
}
return new PostAccountResponse { message = results };
更新1
我可能會嘗試將服務升級到最新版本,並且此新版本的代碼無法再編譯。 我的意思是不起作用。 編譯器在返回的新PostAccountResponse行上引發以下錯誤:
1.-轉換為void返回委托的匿名函數無法返回值。
2.-無法將lambda表達式轉換為預期的委托類型,因為該塊中的某些返回類型不能隱式轉換為委托返回類型。
3.-無法將類型'Account.Model.PostAccountResponse'隱式轉換為'System.Data.IDbCommand'。 存在顯式轉換(您是否缺少演員表?)
4.-無法將類型'Account.Model.PostAccountResponse'隱式轉換為'System.Threading.Tasks.Task'
而不是使用dbFactory.Exec()
您應該在using語句中使用數據庫連接,例如:
using (var db = dbFactory.OpenDbConnection())
using (var dbCmd = db.CreateCommand())
{
dbCmd.CommandType = System.Data.CommandType.StoredProcedure;
dbCmd.CommandText = "stored procedure...";
dbCmd.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("@mobile", request.mobile));
MySql.Data.MySqlClient.MySqlParameter message = new MySql.Data.MySqlClient.MySqlParameter("@message", "");
message.Direction = System.Data.ParameterDirection.Output;
dbCmd.Parameters.Add(message);
gender.Direction = System.Data.ParameterDirection.Output;
r = dbCmd.ExecuteReader();
return new PostAccountResponse() { message = string.Format("{0}", message.Value };
}
您試圖在此處使用原始ADO.NET連接來調用MySql存儲過程,但通常在使用OrmLite API訪問數據庫時,您可以直接使用base.Db
ADO.NET Connection,例如:
return Db.Select<Table>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.