URLs in C#

To get the URL of the current page in ASP.NET, you can use the Request.Url.AbsoluteUri property:

string url = Request.Url.AbsoluteUri;

To get the absolute path of the current page in ASP.NET, you can use the Request.Url.AbsolutePath property:

string path = Request.Url.AbsolutePath;

To get the domain name of the current page in ASP.NET, you can use the Request.Url.Host property:

string domain = Request.Url.Host;

To get the path and parameters of the current page in ASP.NET, you can use the Request.Url.PathAndQuery property:

string pathAndQuery = Request.Url.PathAndQuery;

To get the URL of the current page in ASP.NET Core, you can use the HttpContext.Request.Path property:

string url = HttpContext.Request.Path.ToString();

To get the absolute URL of an action in ASP.NET MVC, you can use the Url.Action() method:

string url = Url.Action("ActionName", "ControllerName", new { id = 1 }, Request.Url.Scheme);

To get the URL without the query string in C#, you can use the Request.Url.GetLeftPart(UriPartial.Path) method:

string urlWithoutQuery = Request.Url.GetLeftPart(UriPartial.Path);

To combine URLs in C#, you can use the Uri class:

Uri uri1 = new Uri("https://www.example.com/");
Uri uri2 = new Uri(uri1, "path/to/page.html");
string combinedUrl = uri2.ToString(); // "https://www.example.com/path/to/page.html"
  1. C# URL manipulation:

    • Basic manipulation of URLs in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page?param=value");
      string modifiedUrl = uri.ToString().Replace("www", "blog");
      
  2. Parsing URLs in C#:

    • Parsing different components of a URL in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page?param=value");
      string scheme = uri.Scheme; // "https"
      string host = uri.Host; // "www.example.com"
      string path = uri.AbsolutePath; // "/path/page"
      
  3. Constructing URLs in C#:

    • Building URLs from individual components in C#.
    • Code snippet:
      UriBuilder builder = new UriBuilder
      {
          Scheme = "https",
          Host = "www.example.com",
          Path = "/path/page",
          Query = "param=value"
      };
      Uri uri = builder.Uri;
      
  4. URL encoding and decoding in C#:

    • Encoding and decoding URL components in C#.
    • Code snippet:
      string originalString = "hello world";
      string encodedString = Uri.EscapeDataString(originalString);
      string decodedString = Uri.UnescapeDataString(encodedString);
      
  5. Relative and absolute URLs in C#:

    • Differentiating between relative and absolute URLs in C#.
    • Code snippet:
      Uri baseUri = new Uri("https://www.example.com");
      Uri absoluteUri = new Uri(baseUri, "/path/page");
      
  6. C# URL parameters handling:

    • Handling parameters in URLs in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page?param1=value1&param2=value2");
      string paramValue = HttpUtility.ParseQueryString(uri.Query).Get("param1");
      
  7. Query string manipulation in C#:

    • Manipulating query strings in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page?param1=value1&param2=value2");
      string updatedQuery = "param3=newValue3";
      Uri updatedUri = new UriBuilder(uri) { Query = updatedQuery }.Uri;
      
  8. Building query parameters for URLs in C#:

    • Building query parameters dynamically in C#.
    • Code snippet:
      Dictionary<string, string> parameters = new Dictionary<string, string>
      {
          { "param1", "value1" },
          { "param2", "value2" }
      };
      string queryString = string.Join("&", parameters.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}"));
      Uri uri = new Uri($"https://www.example.com/path/page?{queryString}");
      
  9. URL path manipulation in C#:

    • Manipulating the path component of a URL in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path1/page");
      string newPath = "/path2/newpage";
      Uri updatedUri = new UriBuilder(uri) { Path = newPath }.Uri;
      
  10. C# URL validation:

    • Validating URLs in C#.
    • Code snippet:
      Uri uri;
      bool isValidUrl = Uri.TryCreate("https://www.example.com", UriKind.Absolute, out uri);
      
  11. Getting host and path from URL in C#:

    • Extracting host and path components from a URL in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page");
      string host = uri.Host; // "www.example.com"
      string path = uri.AbsolutePath; // "/path/page"
      
  12. Working with fragments in C# URLs:

    • Handling fragments (hash) in URLs in C#.
    • Code snippet:
      Uri uri = new Uri("https://www.example.com/path/page#section");
      string fragment = uri.Fragment; // "#section"
      
  13. C# URL escape characters:

    • Understanding and handling URL escape characters in C#.
    • Code snippet:
      string escapedUrl = Uri.EscapeUriString("https://www.example.com/path with spaces");
      
  14. Handling special characters in URLs in C#:

    • Managing special characters in URLs in C#.
    • Code snippet:
      string specialUrl = "https://www.example.com/path with spaces?param=value";
      string encodedSpecialUrl = Uri.EscapeDataString(specialUrl);