Handling URL parameters is a common task in Web and console applications because they often carry basic data for processing user requests. Here, I show how to generally read URL parameters in different C# application types, including ASP.NET Core MVC/Web API, ASP.NET Web Forms, console applications, and ASP.NET Core Minimal API. Each method has its own syntax and usage patterns, as shown in the sample code below.
1.ASP.NET Core MVC/Web API
ASP.NET Core MVC and ASP.NET Core Web API are frameworks for building web applications and APIs using the ASP.NET Core platform. ASP.NET Core MVC is a full-featured framework for building web applications using the Model-View-Controller (MVC) design pattern. ASP.NET Core Web API focuses on building RESTful APIs (HTTP services) to expose application functions and data to various clients through the Web.
In an ASP.NET Core MVC or Web API application, URL parameters can be accessed from route data or query strings.
Option 1. Access URL parameters via route parameters
// in a controller action
public IActionResult Index(int id_param)
{
// 'id_param' is a route parameter
var theParam = id_param.ToString();
return View();
}
Option 2. Access URL parameters via query string parameters
// in a controller action
public IActionResult Index()
{
// 'id_param' is a query string parameter
var queryParam = Request.Query["id_param"].ToString();
return View();
}
2.ASP.NET Web Forms
ASP.NET Web Form is a framework for building dynamic, data-driven Web applications with an emphasis on rapid development and an event-driven programming model. Its development experience is similar to Windows Forms development, but for the Web.
In an ASP.NET Web Forms application, use the Request object in the page’s code-behind file to access query string parameters.
// in the page code-behind
protected override void OnLoad(object sender, EventArgs e)
{
// 'id_param' is a Request.QueryString parameter
string paramValue = Request.QueryString["id_param"];
}
3. Console application
A console application is a lightweight program that runs in a command line interface without a graphical user interface. It is often used to perform simple or back-end tasks, automated processes, file processing, data parsing, automated scripts, etc.
In console applications, URL parameters are usually parsed from the input string.
using System;
using System.Web;
class Program
{
static void Main()
{
string url = "https://example.com?id_param=1";
var uri = new Uri(url);
// 'id_param' is a ParseQueryString parameter
var query = HttpUtility.ParseQueryString(uri.Query);
string id_param = query["id_param"];
Console.WriteLine($"id_param: {id_param}");
}
}
4. ASP.NET Core Minimal API
ASP.NET Core Minimal API is a lightweight ASP.NET Core 6.0 framework for building HTTP-based APIs with minimal setup and coding, and simplifies API creation by reducing boilerplate code, allowing direct Quickly define endpoints at program entry points. I don’t usually use this approach as I prefer to use ASP.NET Core MVC/Web API in my projects, but I thought I’d include it anyway.
Minimal API methods in ASP.NET Core handle routing and query string parameters concisely.
Option 1. Access URL parameters via route parameters
// typically in the context of program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/items/{id_param}", (int id_param) =>
{
// 'id_param' is a route parameter
return Results.Ok($"Item ID: {id_param}");
});
Option 2. Access URL parameters via query string parameters
// typically in the context of program.cs
app.MapGet("https://dev.to/search", (HttpContext context) =>
{
// 'id_param' is a Request.Query context
var query = context.Request.Query;
string id_param = query["id_param"];
return Results.Ok($"id_param: {id_param}");
});
app.Run();
in conclusion
Understanding how to read URL parameters across various application types is critical for me to build powerful and flexible solutions. Whether I’m using a Web-based framework (such as ASP.NET Core) or handling parameters in a simple console application, the approach I outline here provides a solid foundation. By using these techniques, I can efficiently handle user input and implement dynamic functionality tailored to the application’s requirements.