Welcome to our comprehensive guide on Route Parameters in ASP .NET! In this tutorial, we'll walk you through understanding and using route parameters in your ASP .NET applications. By the end of this lesson, you'll be able to handle dynamic URLs with ease. 💡 Pro Tip: Route parameters are crucial for creating SEO-friendly URLs and organizing routes effectively in your ASP .NET projects.
Route parameters allow us to create dynamic routes based on specific parts of the URL. They enable us to build URLs that can change according to the data being requested. For example, consider a URL like /products/123, where 123 is a route parameter.
To create a new controller with a route parameter, follow these steps:
ProductController. In the Add scaffolding to the selected project section, set the Controller class to ProductController and the Specify the name of the action methods to true.Empty and click Add.ProductController.cs file, add a route parameter to the Controller class as follows:using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
public class ProductController : Controller
{
public IActionResult Index(int id)
{
// Your code here
}
}
}Here, id is the route parameter that will accept an integer value from the URL.
Now that we have a controller with a route parameter, let's use it in an action method. Replace the code inside the Index method with the following example:
public IActionResult Index(int id)
{
var product = GetProductById(id);
if (product == null)
{
return NotFound();
}
return View(product);
}
private Product GetProductById(int id)
{
// Your code to fetch the product from the database
}In this example, the action method Index accepts an integer value (id) from the URL, fetches the corresponding product, and returns a view with the product data.
Route parameters can also be customized using constraints and optional parameters.
Constraints are used to define specific rules for route parameters. For example, you can make a route parameter accept only numeric values using a constraint.
public class ProductRoute : RouteAttribute
{
public override RouteData GetRouteData(HttpContext context)
{
RouteData routeData = base.GetRouteData(context);
int id;
if (int.TryParse(routeData.Values["id"] as string, out id))
{
routeData.Values["id"] = id;
}
else
{
routeData.Values.Remove("id");
}
return routeData;
}
}In this example, we created a custom ProductRoute attribute that ensures the id route parameter is a valid integer. You can apply this attribute to your action methods like this:
[ProductRoute]
public IActionResult Index(int id)
{
// Your code here
}Optional route parameters allow URLs to omit the parameter if needed. To define an optional parameter, simply prefix it with a question mark (?).
[Route("products/{id?}")]
public IActionResult Index(int id = 0)
{
// Your code here
}In this example, the Index action method accepts an optional integer parameter id. If the URL includes the id parameter, the provided value will be used; otherwise, the default value (0) will be used.
Which of the following is the correct way to define an optional integer route parameter in ASP .NET?