服务在C#中返回null时显示一些消息?

时间:2013-12-27 12:12:14

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

服务包含以下代码段:

if (lstSite != null && lstSite.Count > 0)
{
    csvBytes = ExportListToCSV(lstSite.Select(x => new { x.User,  
                        x.Date, x.Action, x.Data }).ToList(), ",");
    WebOperationContext.Current.OutgoingResponse.ContentType="text/csv";
    WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"]  
     = "attachment; filename=\"Report_" + DateTime.Now.ToString("yyyy-
                               MM-dd-HH:mm") + ".csv\"";
    return new MemoryStream(csvBytes);
}
else
{
    return null; //Show some message like alert("No Data Found.");
}

当服务包含数据时,这将在浏览器上导出excel,但如果为null,则不显示任何内容。 所以我们可以在其他部分显示一些信息。实际上我使用了页面<Form action="CallToService">标签来调用服务,因此CSV会在页面上生成。无法捕获服务响应并显示一些消息。

1 个答案:

答案 0 :(得分:0)

您需要做的是:

在您呼叫此服务的页面上的

将结果存储在某个变量

  var ResultSet= Service.YourFunction();

然后检查返回的数据是否为空

  if(ResultSet==null)
  {
   // this will show alert message
    ScriptManager.RegisterStartupScript(page, typeof(Page), Guid.NewGuid().ToString(),     "alert('Your message');", true);

  }