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

问题描述 投票:0回答:1

我想使用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文件,作为位于下面的返回流。

    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个回答
0
投票

您可以使用流在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;
    }

这是接口的实现。

enter image description here

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

这是参考链接:

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

© www.soinside.com 2019 - 2024. All rights reserved.