Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To save a website or web page to HTML in C#, load the URL with HTMLDocument, create HTMLSaveOptions when you need custom resource handling, and call document.Save(savePath, options). Use ResourceHandlingOptions to control JavaScript, linked pages, URL restrictions, and saved resources.
Even when internet access is usually available, you may still need an offline copy of a web page for reading, research, archiving, migration, or document processing. Saving a website to HTML lets you keep the page content and related resources available outside the original website.
There are a few reasons why you may want to convert a website to HTML:
This article shows how to save a website or a web page using the Aspose.HTML for .NET API. You can customize the process – save an entire website or save a single web page.
You can use Aspose.HTML for .NET library to convert a website to HTML for offline reading and resource archiving. Use the following steps:
HTMLDocument object from a URL to convert webpage to HTML.HTMLSaveOptions, the process will work with the default save options, as shown in the example below.The following C# example shows how to save a webpage. With default save options, you only save a separate web page with related resources. Please note that only resources that are in the same domain as the site page will be saved.
1// Extract and save a wab page with default save options in C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Prepare a path to save the downloaded file
7string savePath = Path.Combine(OutputDir, "root/result.html");
8
9// Save the HTML document to the specified file
10document.Save(savePath);Using the functionality described in this article, you can save both individual pages and entire websites with resources. To customize the saving process, you can specify the resource handling options for the HTMLSaveOptions object.
The HTMLSaveOptions and ResourceHandlingOptions classes allow you to customize the saving process. For example, you can control the depth of pages that will be handled and save an entire website or only save a single web page.
The table below summarizes the main properties of the ResourceHandlingOptions class. They provide options for handling maximum page depth, applying limits, or handling external resources such as images, CSS files, and JavaScript when loading an HTML document using the API.
| Property | Description |
|---|---|
| Default | Gets or sets an enum representing the default resource handling method. Currently, Save, Ignore, and Embed values are supported. The default value is Save, meaning the resource will be saved as a file. |
| JavaScript | Represents the way scripts are handled. Currently, Save, Ignore, Discard, and Embed values are supported. The default value is Save, meaning the resource will be saved as a file. |
| MaxHandlingDepth | Defines the maximum depth of pages that will be handled. Use this property to control whether Aspose.HTML saves only the current web page, directly linked pages, or a deeper website structure. A depth of 1 means only pages directly referenced from the saved document will be handled. Setting this property to -1 will lead to the handling of all pages. The default value is 0. |
| PageUrlRestriction | Contains information about restrictions applied to URLs of handled pages. The default value is RootAndSubFolders, meaning only resources in the root and subfolders are processed. |
| ResourceUrlRestriction | This property contains information about restrictions applied to URLs of handled resources such as CSS, JavaScript, images, etc. The default value is SameHost, meaning only resources in the same host are processed. |
Aspose.HTML provides the ability to control the logic for saving scripts. They can be saved in separate files, embedded, or thrown out of the resulting document. The following C# example shows how to save a website and embed JavaScript resources into the resulting HTML document.
To save a website from URL and embed JavaScript resources:
HTMLDocument object from a URL to convert a website to HTML.ResourceHandling.Embed value.In the following example, the ResourceHandling.Embed option specifies that any JavaScript resources should be embedded in the HTML document when saved. This means the resulting HTML file will contain all JavaScript resources within the document rather than referencing them as external files.
1// Download website using HTMLSaveOptions in C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Create an HTMLSaveOptions object and set the JavaScript property
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 JavaScript = ResourceHandling.Embed
12 }
13};
14
15// Prepare a path to save the downloaded file
16string savePath = Path.Combine(OutputDir, "rootAndEmbedJs/result.html");
17
18// Save the HTML document to the specified file
19document.Save(savePath, options);The
MaxHandlingDepth property specifies the maximum depth of the HTML document element hierarchy to be loaded and processed. The API will not load or process any elements beyond this depth. Therefore, the MaxHandlingDepth optimizes the performance of the saving process, helping to reduce the memory and processing power required by the API by limiting the number of elements to be processed.
Only the open document and its resources are saved by default as an individual web page, but you can control the handling depth with the MaxHandlingDepth property. The following example shows how to save not only the document but also pages to which it links and whose URL is nested relative to the URL of this page. Let’s look at the C# example when this property is set to 1, which means that only elements up to a depth of 1 in the HTML document hierarchy will be saved:
1// Save a website with limited resource depth using C#
2
3// Load an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Create an HTMLSaveOptions object and set the MaxHandlingDepth property
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 MaxHandlingDepth = 1
12 }
13};
14
15// Prepare the output path for saving the downloaded content
16string savePath = Path.Combine(OutputDir, "rootAndAdjacent/result.html");
17
18// Save the document along with adjacent resources only
19document.Save(savePath, options);Aspose.HTML for .NET provides various options for filtering the URLs of saved pages for a website. The PageUrlRestriction property restricts loading web pages from specific URLs or domains when saving an HTML document.
By default, the PageUrlRestriction property is set to RootAndSubFolders, meaning only pages in the root and subfolders are processed. However, you can set this property to another value – SameHost or None. Setting it to None will allow you to load web pages from any domain whose URLs are on the saved website. Using this property cautiously is essential, as allowing web pages to be loaded from any domain can increase the risk of security vulnerabilities.
In the following example, all pages to which an HTML document refers and those in the same domain will be saved in addition to the document:
1// Save a website with restricted resource URLs using C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Configure HTMLSaveOptions with restricted resource handling
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 MaxHandlingDepth = 1,
12 PageUrlRestriction = UrlRestriction.SameHost
13 }
14};
15
16// Prepare the output path for the saved content
17string savePath = Path.Combine(OutputDir, "rootAndManyAdjacent/result.html");
18
19// Save the HTML document and allowed resources to the specified path
20document.Save(savePath, options);| Goal | Option to review |
|---|---|
| Save only the current page and same-domain resources | Default HTMLSaveOptions behavior |
| Embed JavaScript into the saved HTML | ResourceHandlingOptions.JavaScript = ResourceHandling.Embed |
| Limit how deeply linked pages are processed | ResourceHandlingOptions.MaxHandlingDepth |
| Restrict which linked pages can be loaded | ResourceHandlingOptions.PageUrlRestriction |
| Restrict which resources can be saved | ResourceHandlingOptions.ResourceUrlRestriction |
| Problem | Cause | Solution |
|---|---|---|
| Some CSS, images, or scripts are missing | Resource URL restrictions or default same-host handling prevent downloading them. | Review ResourceUrlRestriction and make sure the resources are allowed by the save options. |
| Linked pages are not saved | MaxHandlingDepth is 0 by default, so only the open document and its resources are saved. | Increase MaxHandlingDepth when you need linked pages to be processed. |
| Too many pages are downloaded | MaxHandlingDepth or PageUrlRestriction is too broad. | Use a smaller depth and restrict page URLs with RootAndSubFolders or SameHost. |
| JavaScript files remain external | JavaScript handling is set to save files separately or uses default behavior. | Set ResourceHandlingOptions.JavaScript to ResourceHandling.Embed when the workflow needs embedded scripts. |
| Saving a remote page fails | The environment blocks network access, certificates, or remote resources. | Check network permissions, proxy settings, and CA certificates in the runtime environment. |
Aspose.HTML for .NET can save a web page and process linked pages and resources according to HTMLSaveOptions and ResourceHandlingOptions. Use MaxHandlingDepth and URL restrictions carefully to control how much of the site is handled.
With default save options, the open document and related resources are saved. Resource handling restrictions determine which resources are processed.
Set ResourceHandlingOptions.JavaScript to ResourceHandling.Embed in HTMLSaveOptions, then call Save(savePath, options).
Keep MaxHandlingDepth low and use PageUrlRestriction to limit handled pages to the root, subfolders, or the same host.
The complete C# examples and data files are available in the Aspose.HTML for .NET GitHub repository.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.