C# 如何为.net核心MVC web应用程序创建和显示PDF文件?

C# 如何为.net核心MVC web应用程序创建和显示PDF文件?,c#,asp.net,asp.net-core-mvc,C#,Asp.net,Asp.net Core Mvc,我有一个asp应用程序,可以创建和显示PDF文件。我需要为.net核心应用程序复制相同的功能。我对.net核心MVC不是很有经验,所以我不知道如何才能做到这一点 DisplayPDF.aspx.cs public partial class DisplayPDF : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // get application id fr

我有一个asp应用程序,可以创建和显示PDF文件。我需要为.net核心应用程序复制相同的功能。我对.net核心MVC不是很有经验,所以我不知道如何才能做到这一点

DisplayPDF.aspx.cs

public partial class DisplayPDF : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // get application id from query string
        string strFile = Request.QueryString["File"];

        if (!string.IsNullOrEmpty(strFile))
        {
            string fileToOpen = string.Empty;

            // get file path to requested application id's pdf file
            string filePath = System.IO.Path.GetDirectoryName(Request.PhysicalApplicationPath) +
                "\\Sessions\\" + Session.SessionID + "\\" + strFile;

            fileToOpen = "sessions/" + Session.SessionID + "/" + strFile;
            if (!System.IO.File.Exists(filePath))
            {
                LabelError.Visible = true;
                LabelError.Text = "The pdf was not generated.  Try the action again.  If the problem persists contact website support.";
            }

            Response.Redirect(fileToOpen);
        }
        else
        {
            // need to have query string parameter
            throw new ApplicationException("Query string parameter is missing");
        }

    }
}
DisplayPDF.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayPDF.aspx.cs" Inherits="Test.WS.App.DisplayPDF" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Display PDF</title>
    <meta name="robots" content="noindex,nofollow" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="LabelError" runat="server" Visible="false"></asp:Label>
    </div>
    </form>
</body>
</html>

基于此Web表单代码,您可以在core mvc中显示pdf,如下所示:

请注意,我的pdf文件放在core的“wwwroot”文件夹下 项目

DisplayPDFController.cs:

 public class DisplayPDFController : Controller
{
    private IWebHostEnvironment _hostingEnvironment;

    public DisplayPDFController(IWebHostEnvironment environment)
    {
        _hostingEnvironment = environment;
    }
    public IActionResult Index()
    {

        return View();
    }
    [HttpPost]
    public string Index(string fileName)
    {
        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, @"Files\" + fileName);
        if (System.IO.File.Exists(filePath))
        {
            return filePath;
        }
        else
        {
            return "Report was not generated.";
        }
    }
    public FileResult ShowPDF(string path)
    {
        var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
        return File(fileStream, "application/pdf");
    }
}
Index.cshtml视图:

@{
布局=空;
}
指数
$(函数(){
$(“#显示”)。单击(函数(){
event.preventDefault();
$.ajax({
键入:“POST”,
数据:{fileName:$(“#Text1”).val()},
url:“/DisplayPDF/Index”,
成功:功能(响应){
如果(响应=='未生成报告'){
警报(响应);
}否则{
window.open(“/DisplayPDF/ShowPDF?path=“+response,”_blank”);
}
},
});
})
})

基于此Web表单代码,您可以在core mvc中显示pdf,如下所示:

请注意,我的pdf文件放在core的“wwwroot”文件夹下 项目

DisplayPDFController.cs:

 public class DisplayPDFController : Controller
{
    private IWebHostEnvironment _hostingEnvironment;

    public DisplayPDFController(IWebHostEnvironment environment)
    {
        _hostingEnvironment = environment;
    }
    public IActionResult Index()
    {

        return View();
    }
    [HttpPost]
    public string Index(string fileName)
    {
        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, @"Files\" + fileName);
        if (System.IO.File.Exists(filePath))
        {
            return filePath;
        }
        else
        {
            return "Report was not generated.";
        }
    }
    public FileResult ShowPDF(string path)
    {
        var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
        return File(fileStream, "application/pdf");
    }
}
Index.cshtml视图:

@{
布局=空;
}
指数
$(函数(){
$(“#显示”)。单击(函数(){
event.preventDefault();
$.ajax({
键入:“POST”,
数据:{fileName:$(“#Text1”).val()},
url:“/DisplayPDF/Index”,
成功:功能(响应){
如果(响应=='未生成报告'){
警报(响应);
}否则{
window.open(“/DisplayPDF/ShowPDF?path=“+response,”_blank”);
}
},
});
})
})