Search code examples
c#asp.net-mvccanonical-link

Replace Request.Url.Query value in Canonical URL


How do I replace the value of Request.Url.Query with String.Empty in the following snippet?

linkCanonical = "<link rel=\"Canonical\" href=\"" + Html.ViewContext.HttpContext.Request.Url + "\" />";

I know it isn't best practice. This is for a temporary fix.


Solution

  • If you want to remove the entire querystring you could use

    Request.Url.Host + Request.Url.AbsolutePath
    

    This takes the host and ONLY the path information

    Using http://www.test.com/mypath.aspx?id=1 as an example it would give you www.test.com/mypath.aspx

    Here is the MSDN documentation as well to help you through the properties.