You can set the current page URL as a canonical tag using below. Here we set the exact URL of the page with a dynamic host name.
for example: if you want to install the canonical tag: http://www.TestWorld.co.uk/about
In the code below, the host name will be dynamic, since http://www.TestWorld.co.uk/ and Request.RawUrl will give the result, in the end we get a purely dynamic canonical url. Note. Here the canonical tag will be dynamically created on the html page, you do not need to create it manually.
HtmlLink canonical = new HtmlLink(); var uri = Request.Url; string hostName = uri.GetLeftPart(UriPartial.Authority); canonical.Href = hostName + Request.RawUrl.ToString(); canonical.Attributes["rel"] = "canonical"; Page.Header.Controls.Add(canonical);
source share