📜  c# Request.Url - C# (1)

📅  最后修改于: 2023-12-03 14:39:43.941000             🧑  作者: Mango

C# Request.Url

Introduction

In C#, the Request.Url property represents the Uniform Resource Identifier (URI) of the current HTTP request. It contains information about the scheme, host name, port number, path, query string, and fragment identifier of the requested resource.

Syntax

The syntax to access the Request.Url property is as follows:

Uri url = Request.Url;
Properties

The Request.Url property is an instance of the System.Uri class, which has the following properties:

  • Scheme: Gets or sets the URI scheme, such as http or https.
  • Host: Gets or sets the host component of this URI.
  • Port: Gets or sets the port number of this URI.
  • AbsolutePath: Gets or sets the absolute path of this URI.
  • Query: Gets or sets any query information included in the URI.
  • Fragment: Gets or sets any fragment identifier included in the URI.
Example

The following code snippet demonstrates how to access and use the Request.Url property in a C# web application:

Uri url = Request.Url;
string scheme = url.Scheme;
string host = url.Host;
int port = url.Port;
string path = url.AbsolutePath;
string query = url.Query;
string fragment = url.Fragment;

Console.WriteLine("Scheme: " + scheme);
Console.WriteLine("Host: " + host);
Console.WriteLine("Port: " + port);
Console.WriteLine("Path: " + path);
Console.WriteLine("Query: " + query);
Console.WriteLine("Fragment: " + fragment);
Conclusion

In this article, we have explored the Request.Url property in C#. It is a useful property that allows developers to access the URI of the current HTTP request and extract information about its scheme, host, port, path, query string, and fragment identifier.