Handling URL parameters is a common task in web and console applications, as they often carry essential data for processing user requests. Here I show how I typically 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 approach comes with 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 with the Model-View-Controller (MVC) design pattern. ASP.NET Core Web API focuses on building RESTful APIs (HTTP services) that expose application functionality and data to various clients over the web.
In ASP.NET Core MVC or Web API applications, URL parameters can be accessed from route data or query strings.
Option 1. Access the URL parameter via a Route Parameter
// 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 the URL parameter via a Query String Parameter
// 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 Form
ASP.NET Web Form is a framework for building dynamic, data-driven web applications with a focus on rapid development and an event-driven programming model. It is a similar development experience to Windows Form development, but for the web.
In an ASP.NET Web Forms application, query string parameters are accessed using the Request object in the page code-behind file.
// 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 typically used for performing simple or backend tasks, automating processes, file processing, data parsing, automation scripts, etc.
In a Console Application, URL parameters are typically parsed from input strings.
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 code and simplifies the creating APIs by reducing boilerplate code allowing the quick definition of endpoints directly in the program's entry point. I do not typically use this approach as I prefer using ASP.NET Core MVC/Web API for my projects, but I thought I would include it anyway.
The Minimal API approach in ASP.NET Core allows for concise handling of route and query string parameters.
Option 1. Access the URL parameter via a Route Parameter
// 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 the URL parameter via a Query String Parameter
// typically in the context of program.cs
app.MapGet("/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();
Conclusion
Understanding how to read URL parameters across various application types is essential for me to build robust and flexible solutions. Whether I’m working with a web-based framework like ASP.NET Core or handling parameters in a simple console application, the methods I’ve outlined here provide a solid foundation. By using these techniques, I can efficiently process user inputs and implement dynamic functionalities tailored to my application's requirements.
Top comments (0)