[英]How to get Response.Write() result in string using method of return type void in asp.net c#
我有一個為Android and ios
編寫的webservice asmx
,與它們很好地溝通,我用來以json
格式返回值的方法和示例方法是:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Get_Rest_Distance(double startlat,double endlat,double startlng, double endlng)
{
double dist= GetDistance(startlat, endlat, startlng, endlng);
JavaScriptSerializer serializer = new JavaScriptSerializer();
HttpContext.Current.Response.Write(serializer.Serialize(dist));
}
該行: HttpContext.Current.Response.Write(serializer.Serialize(dist));
用於從android和ios向api調用提供數據,
現在想從我的aspx
頁面(Code Behind)
調用這個方法,你注意到我的函數的返回類型是void
我添加了我的webserivice
參考並寫了一些類似的行:
localhost.RestData obj = new localhost.RestData();
string testdata= obj.Get_Rest_Distance(33.200526, 34.027379, -87.5441, -88.167831);
但得到錯誤
Cannot implicitly convert type 'void' to 'string'
正如我提到的方法是返回類型void
,現在我想獲取HttpContext.Current.Response.Write(serializer.Serialize(dist));
語句返回或寫入。
注意:不能在方法中使用return
,因為服務處於活動狀態,並且 android 和 ios 版本都在生產環境中工作。
請隨時提出任何問題,如果需要,我會提供更多代碼。
您可以使用類似這樣的輸出參數:
static void OutParameter(out int tmp)
{
ShowValue("OutParameter (pre): ");
tmp = 10;
ShowValue("OutParameter (post): ");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.