【问题标题】:Open or Render Partial View using WCF使用 WCF 打开或呈现局部视图
【发布时间】:2020-08-31 23:04:44
【问题描述】:

我想使用 WCF Web 服务打开部分视图,但我不知道如何开发它。

如果点击服务链接,服务必须是渲染部分视图html代码。下面的代码在 MVC 应用程序中工作,并返回带有参数的路径上的部分视图。

    public ActionResult RaporPaylasim(string kod, string tarih, string optional)
    {
        return PartialView($"~/Views/MailSablon/RaporPaylasim/Kurumsal/{Kullanici.KpSiteKod}/{kod}.cshtml",
            new Dictionary<string, string>
            {
                ["RaporTarih"] = tarih,
                ["Optional"] = optional
            });
    }

在 WCF 网络服务中,有一个可用的方法来打开 pdf 文件作为返回的流,位于下面。

    public Stream DownloadFileBulten(string KategoriKod, string RaporTarih)
    {
        if (KategoriKod == null)
            throw new Exception("Kategori Kod null!");

        if (!DateTime.TryParse(RaporTarih, out DateTime raporTarih))
            throw new Exception("Rapor Tarih null!");

        ArastirmaContract model = new ArastirmaContract();
        try
        {
            using (var Yonetim = new DB.ArastirmaYonetim(Connection))
            {
                model = Yonetim.Detay_v2(new ArastirmaContract
                {
                    RaporTarihDate = raporTarih,
                    KategoriKod = KategoriKod
                });

                if (model.RaporId == null)
                    throw new Exception("Dosya bulunamadı!");
            }

            string path = $"{ConfigurationManager.AppSettings["Bulten-dosyalari"]}\\{model.DosyaAd}";
            FileInfo fileInfo = new FileInfo(path);
            if (!fileInfo.Exists)
                throw new Exception("Dosya bulunamadı!");

            int length;
            byte[] buffer;
            using (FileStream f = new FileStream(path, FileMode.Open))
            {
                length = (int)f.Length;
                buffer = new byte[length];

                int sum = 0, count;
                while ((count = f.Read(buffer, sum, length - sum)) > 0)
                    sum += count;
                f.Close();
            }

            System.ServiceModel.Web.WebOperationContext.Current
                .OutgoingResponse.ContentLength = length;
            System.ServiceModel.Web.WebOperationContext.Current
                .OutgoingResponse.Headers["Content-Disposition"] = "inline; filename=" + model.SistemDosyaAd;
            System.ServiceModel.Web.WebOperationContext.Current
                .OutgoingResponse.ContentType = GetFileContentType(fileInfo.Extension.Replace(".", ""));

            return new MemoryStream(buffer);
        }
        catch (Exception ex)
        {
            HelperUtils.CmsLogger.Error(ex, "DownloadFileBulten");
            throw new Exception("Bir hata oluştu!");
        }
    }

还有实现上述方法的接口。

{
[ServiceContract]
public interface IArastirmaFileService
{
    [OperationContract]
    [WebGet(UriTemplate = "/DownloadFileBulten/{KategoriKod}/{RaporTarih}",
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Stream DownloadFileBulten(string KategoriKod, string RaporTarih);

    [OperationContract]
    [WebGet(UriTemplate = "/DownloadFileRapor/{KategoriKod}/{RaporTarih}/{EnstrumanKod}",
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Stream DownloadFileRapor(string KategoriKod, string RaporTarih, string EnstrumanKod);
}

文件类型必须是html格式,实际结果,有链接,必须打开html文件,如下图。

enter image description here

【问题讨论】:

    标签: c# web-services wcf model-view-controller partial-views


    【解决方案1】:

    你可以在 WCF 中使用流返回 HTML,这是我的演示:

          [OperationContract]
          [WebGet(UriTemplate = "/start")]
          Stream Test();
    

    这是界面。

                 public Stream Test()
        {
            string str = System.IO.File.ReadAllText(@"D:\Ajax.html");                 
            byte[] array = Encoding.ASCII.GetBytes(str);
            MemoryStream stream = new MemoryStream(array);
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return stream;
        }
    

    这是接口的实现。

    这是请求的结果。可以看到服务器返回了一个HTML文件。

    这是参考链接:

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/service-returns-arbitrary-data-using-the-wcf-web

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      相关资源
      最近更新 更多