繁体   English   中英

使用 WCF 打开或渲染局部视图

[英]Open or Render Partial View using WCF

我想使用 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 web 服务中,有一个可用的方法来打开 pdf 文件,返回的 ZF7B44CFAFDD09Z 文件位于下面的 196C5222E7B549。

    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 文件,如下图。

在此处输入图像描述

您可以使用 stream 在 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM