Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to send optional parameters to an action?

Can I send optional parameters (empty strings, null int?'s etc) to an action through a GET request in asp.net mvc? (one sentence question!)

like image 511
Boris Callens Avatar asked Nov 20 '08 01:11

Boris Callens


People also ask

How do you pass an optional parameter to a function?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

How do I send optional parameters in REST API?

You can then use @DefaultValue if you need it: @GET @Path("/job/{param1}/{param2}") public Response method(@PathParam("param1") String param1, @PathParam("param2") String param2, @QueryParam("optional1") String optional1, @QueryParam("optional2") @DefaultValue("default") String optional2) { ... }

How do you send optional parameters in flutter?

Named optional parameters You can call getHttpUrl with or without the third parameter. You must use the parameter name when calling the function. You can specify multiple named parameters for a function: getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) { // ... }


1 Answers

You can do optional parameters with the routing table fairly easily, just specify the defaults in the route of the global.cs file.

So for a search page with an optional query and page you would have something like

RouteTable.Routes.Add(new Route
{
    Url = "Search/[query]/[page]",
    Defaults = new { controller="Search", action="Results", page=1 },
    RouteHandler = typeof(MvcRouteHandler)
});

Default page for your search is then 1.

This example is found here on Scott Gu's blog.

like image 169
Odd Avatar answered Nov 09 '22 14:11

Odd