IronPDF 如何使用 CSHTML 至 PDF (MVC 架構) 如何使用 C# 在 ASP.NET MVC 中將視圖轉換為 PDF Chaknith Bin 更新:7月 27, 2025 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English View 是 ASP.NET 框架中的一個元件,用於在 Web 應用程式中產生 HTML 標記。 它是模型-視圖-控制器(MVC)模式的一部分,常用於 ASP.NET MVC 和 ASP.NET Core MVC 應用程式。 視圖負責透過動態渲染 HTML 內容向使用者呈現資料。 ASP.NET Web 應用程式 (.NET Framework) MVC 是微軟提供的 Web 應用程式框架。 它遵循一種稱為模型-視圖-控制器(MVC)的結構化架構模式,以組織和簡化 Web 應用程式的開發。 模型:管理資料、業務邏輯和資料完整性。 視圖:呈現使用者介面並渲染資訊。 控制器:處理使用者輸入、處理請求,並協調模型和視圖之間的互動。 IronPDF 簡化了在 ASP.NET MVC 專案中從視圖建立 PDF 檔案的過程。 這使得在 ASP.NET MVC 中產生 PDF 變得簡單直接。 快速入門:輕鬆將 ASP.NET MVC 視圖轉換為 PDF 了解如何使用 IronPDF 快速將 ASP.NET MVC 視圖轉換為 PDF 文件。 只需幾行程式碼,即可將 CSHTML 視圖渲染成高品質的 PDF,從而增強應用程式的功能。 IronPDF 簡化了流程,讓各個層級的開發人員都能輕鬆上手。 首先將 IronPDF 整合到您的 ASP.NET Core 專案中,即可輕鬆地從視圖產生 PDF。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronPDF PM > Install-Package IronPdf 複製並運行這段程式碼。 // Install-Package IronPdf.Extensions.Razor var pdf = new IronPdf.ChromePdfRenderer.RenderRazorToPdf(this.ControllerContext); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronPDF,免費試用! 免費試用30天 最小工作流程(5 個步驟) 下載用於在 ASP.NET MVC 中將視圖轉換為 PDF 的 C# 程式庫 為數據添加模型類別 在控制器中建立一個名為"Person"的操作,並使用RenderView方法。 使用 MVC 5 View Scaffolded 添加視圖 下載示例項目以快速開始 IronPDF 擴充包 IronPdf.Extensions.Mvc.Framework 套件是IronPdf主包的擴充。 在 ASP.NET MVC 中,要將視圖渲染為 PDF 文檔,需要 IronPdf.Extensions.Mvc.Framework 和 IronPdf 套件。 安裝 IronPdf.Extensions.Mvc.Framework 套件 使用 NuGet 安裝 安裝 IronPdf.Extensions.Mvc.Framework 套件 nuget.org/packages/IronPdf.Extensions.Mvc.Framework/ 將視圖渲染為 PDF 要將視圖轉換為 PDF 文件,您需要一個 ASP.NET Web 應用程式 (.NET Framework) MVC 專案。 新增模型類 導航至"模型"資料夾 建立一個名為"Person"的新C#類別檔案。此類將作為表示個人資料的模型。 以下佔位符僅供參考: :path=/static-assets/pdf/content-code-examples/how-to/cshtml-to-pdf-mvc-framework-model.cs namespace ViewToPdfMVCSample.Models { public class Person { public int Id { get; set; } public string Name { get; set; } public string Title { get; set; } public string Description { get; set; } } } Namespace ViewToPdfMVCSample.Models Public Class Person Public Property Id() As Integer Public Property Name() As String Public Property Title() As String Public Property Description() As String End Class End Namespace $vbLabelText $csharpLabel 編輯控制器 導航至"Controllers"資料夾並開啟"HomeController"檔案。我們將新增"Persons"操作。 請參考以下程式碼: 在提供的程式碼中,首先創建了ChromePdfRenderer類別。 要使用RenderView方法,您需要為其提供一個 HttpContext,指定"Persons.cshtml"檔案的路徑,並提供一個包含必要資料的 List。 在渲染視圖時,使用者可以選擇使用渲染選項來自訂邊距、新增自訂文字和 HTML 頁首和頁腳,以及將頁碼套用至產生的 PDF 文件。 請注意The PDF document can be downloaded to the machine using the following code: File(pdf.BinaryData, "application/pdf", "viewToPdfMVC.pdf"). using IronPdf; using System.Collections.Generic; using System.Web.Mvc; using ViewToPdfMVCSample.Models; namespace ViewToPdfMVCSample.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } // GET: Person public ActionResult Persons() { // Create a list of Person objects var persons = new List<Person> { new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" }, new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" }, new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" } }; if (HttpContext.Request.HttpMethod == "POST") { // Define the path to the View file var viewPath = "~/Views/Home/Persons.cshtml"; // Instantiate the ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the view to a PDF document PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline"); // Return the generated PDF file return File(pdf.BinaryData, "application/pdf"); } return View(persons); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } } using IronPdf; using System.Collections.Generic; using System.Web.Mvc; using ViewToPdfMVCSample.Models; namespace ViewToPdfMVCSample.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } // GET: Person public ActionResult Persons() { // Create a list of Person objects var persons = new List<Person> { new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" }, new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" }, new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" } }; if (HttpContext.Request.HttpMethod == "POST") { // Define the path to the View file var viewPath = "~/Views/Home/Persons.cshtml"; // Instantiate the ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the view to a PDF document PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline"); // Return the generated PDF file return File(pdf.BinaryData, "application/pdf"); } return View(persons); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } } Imports IronPdf Imports System.Collections.Generic Imports System.Web.Mvc Imports ViewToPdfMVCSample.Models Namespace ViewToPdfMVCSample.Controllers Public Class HomeController Inherits Controller Public Function Index() As ActionResult Return View() End Function ' GET: Person Public Function Persons() As ActionResult ' Create a list of Person objects 'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property: Dim persons_Conflict = New List(Of Person) From { New Person With { .Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer" }, New Person With { .Name = "Bob", .Title = "Mr.", .Description = "Software Engineer" }, New Person With { .Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer" } } If HttpContext.Request.HttpMethod = "POST" Then ' Define the path to the View file Dim viewPath = "~/Views/Home/Persons.cshtml" ' Instantiate the ChromePdfRenderer Dim renderer As New ChromePdfRenderer() ' Render the view to a PDF document Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict) ' Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline") ' Return the generated PDF file Return File(pdf.BinaryData, "application/pdf") End If Return View(persons_Conflict) End Function Public Function About() As ActionResult ViewBag.Message = "Your application description page." Return View() End Function Public Function Contact() As ActionResult ViewBag.Message = "Your contact page." Return View() End Function End Class End Namespace $vbLabelText $csharpLabel 透過RenderView方法取得PdfDocument物件後,您可以對其進行各種改進和調整。 您可以將 PDF 轉換為PDFA或PDFUA格式,對建立的 PDF 進行數位簽名,或根據需要合併和分割PDF 文件。 此外,該庫還允許您旋轉頁面、插入註釋或書籤,以及為 PDF 文件添加獨特的浮水印。 新增視圖 右鍵單擊新新增的"人員"操作,然後選擇"新增視圖"。 !右鍵點選"人物"操作 為新的 Scaffolded 專案選擇"MVC 5 視圖"。 !選擇腳手架 選擇"清單"範本和"人員"模型類別。 這將會建立一個名為"Persons"的.cshtml檔案。 導覽至"Views"資料夾 -> "Home"資料夾 -> "Persons.cshtml"檔案。 若要新增一個可以呼叫"人員"操作的按鈕,請使用以下程式碼: @using (Html.BeginForm("Persons", "Home", FormMethod.Post)) { <input type="submit" value="Print Person" /> } @using (Html.BeginForm("Persons", "Home", FormMethod.Post)) { <input type="submit" value="Print Person" /> } HTML 在頂部導覽列新增一個版塊 在"Views"資料夾中,導覽至"Shared"資料夾 -> "_Layout.cshtml"檔案。將"Person"導覽項目放在"Home"之後。 確保 ActionLink 方法的值與我們的檔案名稱完全匹配,在本例中檔案名稱為"Persons"。 <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark"> <div class="container"> @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li> </ul> </div> </div> </nav> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark"> <div class="container"> @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li> </ul> </div> </div> </nav> HTML 運行專案 這將向您展示如何運行專案並產生 PDF 文件。 輸出 PDF 下載 ASP.NET MVC 項目 您可以下載本指南的完整程式碼。它以壓縮檔案的形式提供,您可以在 Visual Studio 中將其作為 ASP.NET Web 應用程式 (.NET Framework) MVC 專案開啟。 下載用於 PDF 轉換的 MVC 範例項目 常見問題解答 如何在 ASP.NET Core MVC 中將 View 轉換為 PDF? 您可以使用 IronPDF library 在 ASP.NET Core MVC 中將 View 轉換為 PDF。透過 NuGet 安裝 IronPdf.Extensions.Mvc.Framework 套件,並使用 ChromePdfRenderer 類將視圖渲染為 PDF 文件。 在 ASP.NET MVC 中,將視圖繪製成 PDF 文件需要哪些條件? 要在 ASP.NET MVC 中將視圖繪製成 PDF 文件,您需要 IronPDF 函式庫和 ChromePdfRenderer 類,它需要 HttpContext 、視圖路徑和資料清單來產生 PDF。 如何在 ASP.NET 應用程式中使用 IronPDF 自訂 PDF 輸出? IronPDF 允許使用 RenderingOptions 自訂 PDF 輸出。您可以調整頁邊空白、新增自訂的頁首與頁尾,並包含頁碼,以根據您的需求量身打造 PDF 文件。 在 ASP.NET MVC 中為生成 PDF 設定模型類別涉及哪些步驟? 若要在 ASP.NET MVC 中設定模型類別,請導航至「模型」資料夾,建立新的 C# 類別檔案,並定義類別的屬性,以代表您需要用來產生 PDF 的資料結構。 如何在 ASP.NET MVC 中將 PDF 生成按鈕整合到 View 中? 要在 ASP.NET MVC 中將 PDF 生成按鈕整合到 View 中,您可以在 View 的 HTML 標記中添加按鈕元素,並將其連結到控制器中的動作,該動作使用 IronPDF for .NET 將 View 呈現為 PDF。 在 ASP.NET MVC 中新增檢視的流程是什麼? 若要在 ASP.NET MVC 中新增 View,請在控制器中所需的動作上按一下滑鼠右鍵,選擇「新增 View」,選擇「MVC 5 View」作為支架項目,並選擇適當的範本和模型類別。 我在哪裡可以找到 ASP.NET MVC 中 View 轉換為 PDF 的範例專案? 您可以從 IronPDF 網站下載 PDF 轉換的完整 MVC 範例專案,其中包含可在 Visual Studio 中開啟的壓縮檔,以便實際執行。 如何安裝 IronPDF 以便在 ASP.NET MVC 專案中使用? 在 ASP.NET MVC 專案中安裝 IronPDF,方法是使用 NuGet Package Manager 並執行下列指令:Install-Package IronPdf.Extensions.Mvc.Framework. 如果在 ASP.NET MVC 中生成 PDF 失敗,有哪些常見的故障排除步驟? 如果在 ASP.NET MVC 中生成 PDF 失敗,請確認已正確安裝 IronPDF 套件,檢查傳給 ChromePdfRenderer 的路徑和上下文,並驗證已正確渲染檢視和資料。 IronPDF 與 .NET 10 相容嗎?升級 IronPDF 會帶來哪些好處? 是-IronPDF 與 .NET 10 完全相容。升級可帶來運行時的改進,如減少堆分配、改善記憶體使用、增強 HTML 到 PDF 渲染的性能,以及存取新的語言功能和框架增強功能。 Chaknith Bin 立即與工程團隊聊天 軟體工程師 Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。 準備好開始了嗎? Nuget 下載 16,493,056 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:16,493,056 檢視授權