aspnet-mvc
  1. aspnet-mvc-route-parameters-and-optional-parameters

Route Parameters and Optional Parameters - (ASP.NET MVC Routing)

Routing is an essential part of building ASP.NET MVC applications. It helps you map URLs to their respective controllers and actions. In this tutorial, we'll discuss route parameters and optional parameters in ASP.NET MVC routing.

Syntax

Route parameters are defined by enclosing a parameter name in curly braces {}. Optional parameters are defined by enclosing a parameter name in parentheses ().

routes.MapRoute(
    name: "MyRoute",
    url: "products/{category}/{productName}/{id}",
    defaults: new { controller = "Home", action = "Details", id = UrlParameter.Optional }
);

Example

public class ProductsController : Controller
{
    public ActionResult Details(string category, string productName, int? id)
    {
        // do something with the parameters
        return View();
    }
}

In this example, we define a route that maps to the Details action on the ProductsController. The route contains three parameters: category, productName, and id. The id parameter is optional, as indicated by the UrlParameter.Optional value in the default route.

When a request is made to the URL products/electronics/laptop, the category parameter will be set to "electronics" and the productName parameter will be set to "laptop". Since the id parameter is optional, it will be null.

Explanation

Route parameters allow you to capture values from the URL and pass them to the controller action method. In the example above, we have defined three route parameters: category, productName, and id. When a request is made to a URL that matches this route, the values from the URL are assigned to the corresponding parameters.

Optional parameters are defined by specifying UrlParameter.Optional for the default value of the parameter. This tells the routing engine that the parameter is optional and should be assigned the default value if it is not present in the URL.

Use

Route parameters and optional parameters are useful in situations where you need to capture dynamic values from the URL and pass them to your controller actions. For example, in an e-commerce application, you might need to capture the product category, product name, and product ID from the URL in order to display information about the product on a details page.

Important Points

Here are some important points to keep in mind when using route parameters and optional parameters in ASP.NET MVC:

  • Route parameters can be used to capture dynamic values from the URL and pass them to your controller actions.
  • Optional parameters are defined using UrlParameter.Optional as the default value.
  • If a non-optional parameter is not present in the URL, a 404 error will be returned.

Summary

In this tutorial, we discussed route parameters and optional parameters in ASP.NET MVC routing. We covered the syntax, example, explanation, use and important points of route parameters and optional parameters. With this knowledge, you can define custom routes in your ASP.NET MVC applications that capture dynamic values from the URL and pass them to your controller actions.

Published on: