Get a Canonical URL from an HTML Document in .NET Core

Cloudmersive
2 min readJul 28, 2021

Obtaining the canonical URL of an HTML document will assist you in establishing if you are viewing the root version of a web page. This is helpful information for those attempting to put together sitemaps or for search engines that want to avoid duplicate content. In this tutorial, we will provide instructions on how you can use an API in .NET Core to automatically retrieve the canonical URL of a specific HTML document.

Let’s start things off by running this command to install the SDK:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, we can call the function with the following code:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;
namespace Example
{
public class EditHtmlHtmlGetRelCanonicalExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditHtmlApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Optional: Input file to perform the operation on. (optional)
var inputFileUrl = inputFileUrl_example; // string | Optional: URL of a file to operate on as input. (optional)
try
{
// Gets the rel canonical URL for the HTML document
HtmlGetRelCanonicalUrlResult result = apiInstance.EditHtmlHtmlGetRelCanonical(inputFile, inputFileUrl);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditHtmlApi.EditHtmlHtmlGetRelCanonical: " + e.Message );
}
}
}
}

If you need to retrieve an API key for the operation, you can do so by registering for a free account on the Cloudmersive website; this will provide 800 calls/month across any of our APis.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.