【问题标题】:Consuming .asmx service in asp.net web api application在 asp.net web api 应用程序中使用 .asmx 服务
【发布时间】:2016-03-28 19:15:36
【问题描述】:

我在我的 asp.net web api 应用程序中使用 .asmx 服务,如下所示:

[

但是这个服务什么也不返回,而是在 HTTP 响应对象中写入响应,如下所示: ` [WebMethod(EnableSession = true)] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 公共无效GetResearchDocsBySector(int pageCode,int resultsPerPage,字符串subTypeIds,int timeframe) { JavaScriptSerializer js = new JavaScriptSerializer(); IRatingsDirectDataMgr dataMgr = RemoteUtil.GetRemote();

        List<ResearchDocumentDisplay> results = dataMgr.GetSolrResults(pageCode, resultsPerPage, subTypeIds, timeframe, false);

        List<ResearchDocumentWidgetDisplay> resultList = new List<ResearchDocumentWidgetDisplay>();
        foreach (var item in results)
        {
            var obj = ObjMapper<ResearchDocumentDisplay, ResearchDocumentWidgetDisplay>.Map(item);
            obj.ArticleTypeName = Constants.TypeMappings[obj.ArticleTypeId];
            resultList.Add(obj);
        }

        HttpContext.Current.Response.Write(js.Serialize(resultList));
    }`

我想在我的 webapi 应用程序中以 json 格式使用从服务获得的结果,我们该怎么做?

注意:我根本无法更改 .asmx 服务代码!!

【问题讨论】:

  • 请勿将代码发布为图片。我们不使用图片进行编程。图片使在答案中重新利用您的代码变得困难,它们无法编辑,也无法搜索。请编辑您的问题以更正此问题。

标签: c# asp.net web-services wcf asp.net-web-api


【解决方案1】:

设置 GetResearchDocsBySector 以返回您的列表,而不是使用 void 返回类型并将其注入当前 http 内容。为此,您需要将 ResearchDocumentWidgetDisplay 标记为 Serialisable,您可以在 ResearchDocumentWidgetDisplay 类上方添加 [Serialisable]。

【讨论】:

  • 我无法编辑服务,所以服务代码只能这样保留
  • 它可能归结为您的呼叫没有返回任何结果的服务。你确定你传递的参数返回一个结果集吗?其次,我建议在请求上运行 Fiddler 以确保结果被传递。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多