跳過到頁腳內容
.NET幫助

Grapevine .NET(對開發者如何理解的工作)

Grapevine 是一個直接但有效的 C# 微型網頁框架。 它提供程式設計師一個快速、簡易且適應性強的方法來建立 RESTful 網路服務。 Grapevine 非常適合小型專案和原型,因為它具有可擴充性和易用性。 此外,它還必須具備足夠的穩定性,以處理更複雜的應用程式。

借助著名的 .NET 套件 IronPDF,開發人員可以從 PDF 文件中產生、修改和擷取內容。 IronPDF 豐富的功能集使在 C# 應用程式中處理 PDF 變得更加容易,因此成為許多開發人員的重要工具。

透過在您的線上服務中實現流暢的 PDF 建立和操作,結合 Grapevine 和 IronPDF 可以大大改善您的 C# Web 應用程式。 此連結提供了一種有效的方式來建立發票、報告或任何其他類型的文件。

什麼是 C#?

Grapevine 這個 C# 微型 Web 框架的協助下,可以建立輕量、快速、適應性強的 RESTful Web 服務和 API。 它非常適合需要有效處理 HTTP 請求的微小專案、原型和微服務,這都歸功於它簡單的模式和基本架構,保證了快速的設定和優異的效能。

Grapevine 透過提供彈性且易於使用的路由機制,讓識別和管理端點的過程變得更容易。 它透過支援多種 HTTP 方法(包括 GET、POST、PUT 和 DELETE),簡化了 CRUD 操作和組織良好的 RESTful API 的建立。 它也支援中間件,讓程式設計師能夠透過建立自訂元件來增加更多功能,如記錄、驗證和錯誤處理。

Grapevine .NET (How It Works For Developers):圖 1

由於此架構遵循 REST 原則,因此開發人員可以建立可擴充且可靠的 API。它的簡單性及易用性使其成為需要快速建立原型並啟動 Web 服務的開發人員的最佳選擇,而無需處理大型框架的複雜性。 輕量級的設計使 Grapevine 適用於不需要複雜 Web 框架的微服務和應用程式。

葡萄藤 C# 的特點

輕量且快速。

Grapevine 的基本設計可確保小體積和優異的效能,因此非常適合對速度要求極高的應用程式。

直觀的路由。

該架構透過提供簡單易用的路由系統,簡化了 HTTP 端點的定義與管理。

RESTful API 設計

Grapevine 是以 REST 概念開發,支援各種 HTTP 方法,包括 GET、POST、PUT 和 DELETE,有助於建立可靠且有組織的 API。

中介軟體支援

自訂的中介軟體元件,有助於日誌、驗證、授權和錯誤處理等工作,讓開發人員可以擴充 Grapevine 的功能。

易用性

Grapevine 簡單的設計讓設定與開發快速完成,非常適合網路服務原型與部署。

相容性

其與其他 .NET 函式庫和工具的相容性使其在各種應用場景中都能發揮作用。

模組化

該架構的模組化設計可讓開發人員只納入必要的功能,以維持應用程式的效率與精簡。

可擴展性

Grapevine 可適用於各種專案規模,儘管其屬於輕量級,但仍可擴充以容納更複雜的應用程式。

社群與支援

充滿活力的 Grapevine 社群提供工具、指導和案例研究,協助開發人員充分利用框架。

彈性配置

該架構廣泛的組態選項可讓開發人員調整 REST 伺服器和設定,以符合特定需求。

建立與設定 C

建立開發環境、安裝所需的套件以及設定 Grapevine 架構是建立與設定 Grapevine C# 專案所涉及的部分階段。 以下是讓您入門的逐步教學:

建立新的 .NET 專案。

1.開啟指令提示或終端機。 2.輸入以下字元啟動新建立的 .NET 主控台應用程式:

dotnet new console -n GrapevineExample
cd GrapevineExample
dotnet new console -n GrapevineExample
cd GrapevineExample
SHELL

安裝葡萄藤

1.將 Grapevine 套件加入您的專案:

dotnet add package Grapevine
dotnet add package Grapevine
SHELL

配置 Grapevine

using Grapevine;
using Grapevine.Interfaces.Server;
using Grapevine.Server;
using Grapevine.Server.Attributes;
using Grapevine.Shared;

public class Program
{
    public static void Main(string[] args)
    {
        // Create and start a new REST server
        var server = new RestServer();
        server.Start();
        Console.WriteLine("Server is running...");
        Console.WriteLine("Press enter to stop the server.");
        Console.ReadKey();
        server.Stop();
    }
}

[RestResource]
public class SampleResource
{
    // Defines a route method for the /hello endpoint
    [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/hello")]
    public IHttpContext HelloWorld(IHttpContext context)
    {
        // Send a response for the GET request at /hello
        context.Response.SendResponse("Hello, World!");
        return context;
    }
}
using Grapevine;
using Grapevine.Interfaces.Server;
using Grapevine.Server;
using Grapevine.Server.Attributes;
using Grapevine.Shared;

public class Program
{
    public static void Main(string[] args)
    {
        // Create and start a new REST server
        var server = new RestServer();
        server.Start();
        Console.WriteLine("Server is running...");
        Console.WriteLine("Press enter to stop the server.");
        Console.ReadKey();
        server.Stop();
    }
}

[RestResource]
public class SampleResource
{
    // Defines a route method for the /hello endpoint
    [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/hello")]
    public IHttpContext HelloWorld(IHttpContext context)
    {
        // Send a response for the GET request at /hello
        context.Response.SendResponse("Hello, World!");
        return context;
    }
}
Imports Grapevine
Imports Grapevine.Interfaces.Server
Imports Grapevine.Server
Imports Grapevine.Server.Attributes
Imports Grapevine.Shared

Public Class Program
	Public Shared Sub Main(ByVal args() As String)
		' Create and start a new REST server
		Dim server = New RestServer()
		server.Start()
		Console.WriteLine("Server is running...")
		Console.WriteLine("Press enter to stop the server.")
		Console.ReadKey()
		server.Stop()
	End Sub
End Class

<RestResource>
Public Class SampleResource
	' Defines a route method for the /hello endpoint
	<RestRoute(HttpMethod := HttpMethod.GET, PathInfo := "/hello")>
	Public Function HelloWorld(ByVal context As IHttpContext) As IHttpContext
		' Send a response for the GET request at /hello
		context.Response.SendResponse("Hello, World!")
		Return context
	End Function
End Class
$vbLabelText   $csharpLabel

第一步是從 Grapevine 函式庫中匯入所需的命名空間,以協助 HTTP 請求處理、路由和伺服器操作。 Main 類別中的 Program 函數建立並啟動一個 RestServer 對象,該物件監聽新的 HTTP 請求。

使用者可透過主控台得知伺服器已啟動並正在執行,若按下任何鍵就會終止。 端點由 SampleResource 類別定義,並標記有 [RestResource] 屬性,該屬性還指定了 HelloWorld 函數,該函數回應在 /hello 位置發出的 GET 請求。

當成功命中端點時,HelloWorld 方法使用 IHttpContext 參數向客戶端傳回"Hello, World!"回應,以及有關 HTTP 請求和回應的詳細資訊。 這個簡單的配置示範了如何建立一個單路由、輕量級的 Web 伺服器,突顯了 Grapevine 在 C# 中的 HTTP 請求處理的人性化功能。

Grapevine .NET (How It Works For Developers):圖 2

開始

建立一個專案,讓您可以使用 Grapevine 建立 RESTful 網路服務,並使用 IronPDF 建立或修改 PDF 文件,這是開始使用 C# 中的 Grapevine 和 IronPDF 的第一步。 以下是一步一步的教學,讓您能輕鬆上手:

什麼是 IronPDF?

C# 程式可以利用功能豐富的 .NET 函式庫 IronPDF 來建立、讀取和編輯 PDF 文件。 開發人員可以使用此工具輕鬆地將 HTML、CSS 和 JavaScript 內容轉換為高品質、可列印的 PDF。 新增頁首和頁尾,分割和合併 PDF,為文件加上水印,以及將 HTML 轉換為 PDF,這些都是 IronPDF 可以完成的重要工作。 IronPDF 同時支援 .NET Framework 和 .NET Core,因此適用範圍廣泛。

由於 PDF 易於使用且能提供豐富的資訊,因此開發人員可以輕鬆地將其包含在產品中。 IronPDF 能夠處理複雜的佈局和格式,這意味著它所建立的 PDF 與原始 HTML 文字非常相似。

Grapevine .NET (How It Works For Developers):圖 4

IronPDF 的特點

從 HTML 產生 PDF

將 HTML、CSS 及 JavaScript 轉換為 PDF。 IronPDF 支援媒體查詢和回應式設計等現代網路標準,有助於使用 HTML 和 CSS 來動態設定 PDF 發票、報表和文件的樣式。

PDF編輯

可以在現有的 PDF 中加入文字、圖片和其他素材。 使用 IronPDF 可擷取文字和圖像,將多個 PDF 合併為單一文件,將 PDF 文件分割為獨立文件,並為 PDF 頁面新增頁眉、頁腳、註解和水印。

PDF 轉檔

將 Word、Excel 和影像檔案轉換為 PDF,或反過來將 PDF 文件轉換為影像格式 (PNG、JPEG 等)。

效能與可靠性

高性能和可靠性是工業環境下理想的設計特質,可輕鬆處理大型文件集。

安裝 IronPDF

安裝 IronPDF 套件,即可取得在 .NET 專案中處理 PDF 的工具。

dotnet add package IronPdf
dotnet add package IronPdf
SHELL

使用 IronPDF 閱讀。

要讓第一個伺服器在程式中處理 HTTP 請求和回應,必須透過執行 Program.cs 檔案來初始化 Grapevine RestServer 執行個體。伺服器透過 REST 伺服器的 Start()Stop() 方法來管理,並可透過控制台指令在按下某個鍵時暫停伺服器。

using Grapevine.Interfaces.Server;
using Grapevine.Server.Attributes;
using Grapevine.Server;
using Grapevine.Shared;
using IronPdf;
using System.Threading.Tasks;
using System;

class Program
{
    [RestResource]
    public class PdfResource
    {
        // Route method for PDF generation
        [RestRoute(HttpMethod = Grapevine.Shared.HttpMethod.GET, PathInfo = "/generate-pdf")]
        public IHttpContext GeneratePdf(IHttpContext context)
        {
            // HTML content to be converted to PDF
            var htmlContent = "<h1>Hello, PDF!</h1><p>This is a PDF generated using IronPDF.</p>";

            // Create a new PDF renderer
            var renderer = new ChromePdfRenderer();

            // Render the PDF from the HTML content
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Convert PDF to byte array
            var pdfBytes = pdf.BinaryData;

            // Set response content type and length
            context.Response.ContentType = ContentType.CUSTOM_BINARY;
            context.Response.ContentLength64 = pdfBytes.Length;

            // Send PDF byte array as response
            context.Response.SendResponse(pdfBytes);

            return context;
        }
    }

    static async Task Main(string[] args)
    {
        // Create and start a new REST server
        var server = new RestServer();
        server.LogToConsole().Start();
        Console.WriteLine("Server is running...");
        Console.WriteLine("Press any key to stop the server.");
        Console.ReadKey();
        server.Stop();
    }
}
using Grapevine.Interfaces.Server;
using Grapevine.Server.Attributes;
using Grapevine.Server;
using Grapevine.Shared;
using IronPdf;
using System.Threading.Tasks;
using System;

class Program
{
    [RestResource]
    public class PdfResource
    {
        // Route method for PDF generation
        [RestRoute(HttpMethod = Grapevine.Shared.HttpMethod.GET, PathInfo = "/generate-pdf")]
        public IHttpContext GeneratePdf(IHttpContext context)
        {
            // HTML content to be converted to PDF
            var htmlContent = "<h1>Hello, PDF!</h1><p>This is a PDF generated using IronPDF.</p>";

            // Create a new PDF renderer
            var renderer = new ChromePdfRenderer();

            // Render the PDF from the HTML content
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Convert PDF to byte array
            var pdfBytes = pdf.BinaryData;

            // Set response content type and length
            context.Response.ContentType = ContentType.CUSTOM_BINARY;
            context.Response.ContentLength64 = pdfBytes.Length;

            // Send PDF byte array as response
            context.Response.SendResponse(pdfBytes);

            return context;
        }
    }

    static async Task Main(string[] args)
    {
        // Create and start a new REST server
        var server = new RestServer();
        server.LogToConsole().Start();
        Console.WriteLine("Server is running...");
        Console.WriteLine("Press any key to stop the server.");
        Console.ReadKey();
        server.Stop();
    }
}
Imports Grapevine.Interfaces.Server
Imports Grapevine.Server.Attributes
Imports Grapevine.Server
Imports Grapevine.Shared
Imports IronPdf
Imports System.Threading.Tasks
Imports System

Friend Class Program
	<RestResource>
	Public Class PdfResource
		' Route method for PDF generation
		<RestRoute(HttpMethod := Grapevine.Shared.HttpMethod.GET, PathInfo := "/generate-pdf")>
		Public Function GeneratePdf(ByVal context As IHttpContext) As IHttpContext
			' HTML content to be converted to PDF
			Dim htmlContent = "<h1>Hello, PDF!</h1><p>This is a PDF generated using IronPDF.</p>"

			' Create a new PDF renderer
			Dim renderer = New ChromePdfRenderer()

			' Render the PDF from the HTML content
			Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)

			' Convert PDF to byte array
			Dim pdfBytes = pdf.BinaryData

			' Set response content type and length
			context.Response.ContentType = ContentType.CUSTOM_BINARY
			context.Response.ContentLength64 = pdfBytes.Length

			' Send PDF byte array as response
			context.Response.SendResponse(pdfBytes)

			Return context
		End Function
	End Class

	Shared Async Function Main(ByVal args() As String) As Task
		' Create and start a new REST server
		Dim server = New RestServer()
		server.LogToConsole().Start()
		Console.WriteLine("Server is running...")
		Console.WriteLine("Press any key to stop the server.")
		Console.ReadKey()
		server.Stop()
	End Function
End Class
$vbLabelText   $csharpLabel

在 Grapevine 配置中定義了一個 PdfResource 類,用於管理與 PDF 建立相關的特定 HTTP 請求。 如果存在 [RestResource] 屬性,則表示此類具有可以回應 RESTful 路由的方法。

Grapevine .NET (How It Works For Developers):圖 5

[RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/generate-pdf")] 註解位於 GeneratePdf 方法中,表示該方法回應 /generate-pdf 端點的 GET 請求。 在程序內:

  • 將轉換為 PDF 的內容由 HTML 內容字串 (htmlContent) 表示,該字串已定義。 使用 IronPDF 的 ChromePdfRenderer 從 HTML 文字建立 PDF 文件 (pdf)。
  • 從最終的 PDF 文件建立一個位元組數組 (pdfBytes)。
  • HTTP 上下文 (context) 設定為以 PDF 內容類型進行回复,並且它使用 SendResponse 將 PDF 位元組數組傳回客戶端。

Grapevine .NET (How It Works For Developers):圖 6

結論

總而言之,希望在其應用程式中整合 Web 服務功能與動態 PDF 製作的開發人員,將可在 Grapevine C# 與 IronPDF 的整合中找到穩固的解決方案。 RESTful 端點的設定與實作透過 Grapevine 輕量且友善的極簡網頁框架得以簡化,同時也方便處理 HTTP 請求與建立自訂路由。 IronPDF 則可增強應用程式的功能,讓 HTML 資訊輕鬆轉換成優異的 PDF 文件,包括 JavaScript 整合和 CSS 造型支援。

此連結可讓開發人員建立動態、專業外觀的 PDF 並加以散佈,同時簡化互動式資料驅動網路服務的建立。 Grapevine 和 IronPDF 共同提供了一個靈活的工具箱,滿足當代線上應用程式開發的需求,無論是用於製作報表、發票或其他文件類型。 透過結合 IronPDF 強大的 PDF 創作能力和 Grapevine 友善的使用者介面,開發人員可以建立可擴充的有效解決方案,滿足廣泛的使用者需求和企業目標。

有了 IronPDF 以及 Iron Software 的支援,開發人員可以獲得更多 Web 應用程式和功能,以及更有效率的開發。 為了達到這個目的,它將全面的支援與極其靈活的 Iron Software 系統和套件融合在一起,提供明確定義的專案專用授權選項,讓開發人員可以輕鬆選擇最佳化的模型。 這些優點可讓開發人員快速、連貫且有效地實作解決方案,以因應各種挑戰。

常見問題解答

如何在 C# 中創建 RESTful 網路服務?

您可以使用 Grapevine, 一個 C# 微型網路框架,快速輕鬆地創建 RESTful 網路服務。它提供直觀的路由和中介軟件支持,以實現自定義功能。

Grapevine 為什麼適合作為小型項目和原型的選擇?

Grapevine 輕量且可擴展,使其成為小型項目和原型的理想選擇。其易用的設計和模組化使開發者能夠構建高效和適應性強的網路服務,而不需要多餘的負擔。

如何在 C# 應用程序中生成來自 HTML 內容的 PDF?

您可以使用 IronPDF 將 HTML 內容轉換為高質量的 PDF 文檔。IronPDF 支持 HTML、CSS 和 JavaScript,讓您輕鬆地從網路內容生成專業外觀的 PDF。

使用 Grapevine 和 IronPDF 有哪些好處?

結合使用 Grapevine 和 IronPDF 讓開發者可以高效地創建和操作 PDF 的網路服務。這種整合特別適合需要生成文件(如發票和報告)的應用程序。

Grapevine 提供了哪些功能來開發 RESTful API?

Grapevine 提供直觀路由、中介軟件支持和 RESTful API 設計等功能。這些功能簡化了開發過程,允許構建可擴展且有條理的網路服務。

中介軟件如何增強 C# 網路框架的功能?

在 Grapevine 中,中介軟件允許開發者擴展框架功能,通過實施自定義組件來處理日誌記錄、身份驗證和錯誤處理等任務。

IronPDF 能處理複雜的 PDF 文檔佈局嗎?

是的,IronPDF 可以通過支持現代網路標準(如媒體查詢和響應性設計)來處理複雜佈局。這對於使用 HTML 和 CSS 動態設計 PDF 非常有用。

哪些類型的應用程序可以從 Grapevine 和 IronPDF 的結合中受益?

需要動態網路服務和 PDF 生成功能的應用程序,例如生成報告或發票的應用程序,可以大大受益於 Grapevine 和 IronPDF 的結合使用。

Jacob Mellor, Team Iron 首席技術官
首席技術官

Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我