【发布时间】: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文件,如下图。
【问题讨论】:
标签: c# web-services wcf model-view-controller partial-views