简体   繁体   中英

Open or Render Partial View using WCF

I want to open partial view using WCF web service but I do not know how to develop it.

If click service link, service must be render partial view html code. Below code works in MVC application and return partial view on the path with parameters.

    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
            });
    }

In WCF web service, there is an available method to open pdf file as returned stream at located below.

    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!");
        }
    }

Also there is an interface to implement above method.

{
[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);
}

File type must be html format, Actual result, there is a link, must open html file like below picture.

enter image description here

You can use stream to return HTML in WCF,Here is my demo:

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

This is the interface.

             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;
    }

This is the implementation of the interface.

在此处输入图像描述

This is the result of the request.You can see that the server returned an HTML file.

This is the reference link:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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